{"id":2289,"date":"2025-03-08T07:02:22","date_gmt":"2025-03-08T07:02:22","guid":{"rendered":"https:\/\/mailitics.com\/index.php\/2025\/03\/08\/comprehensive-guide-to-dependency-management-in-python\/"},"modified":"2025-03-08T07:02:22","modified_gmt":"2025-03-08T07:02:22","slug":"comprehensive-guide-to-dependency-management-in-python","status":"publish","type":"post","link":"https:\/\/mailitics.com\/index.php\/2025\/03\/08\/comprehensive-guide-to-dependency-management-in-python\/","title":{"rendered":"Comprehensive Guide to Dependency Management in Python"},"content":{"rendered":"<p>    Comprehensive Guide to Dependency Management in Python<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n    <!-- no image --><br \/>\n \t<BR><br \/>\n<BR><\/BR><\/p>\n<div>\n<h1 class=\"wp-block-heading\" id=\"0047\">Introduction<\/h1>\n<p class=\"wp-block-paragraph\" id=\"ef43\">When learning Python, many beginners focus solely on the language and its libraries while completely ignoring virtual environments. As a result, managing Python projects can become a mess: dependencies installed for different projects may have conflicting versions, leading to compatibility issues.<\/p>\n<p class=\"wp-block-paragraph\" id=\"ae58\">Even when I studied Python, nobody emphasized the importance of virtual environments, which I now find very strange. They are an extremely useful tool for isolating different projects from each other.<\/p>\n<p class=\"wp-block-paragraph\" id=\"7876\">In this article, I will explain how virtual environments work, provide several examples, and share useful commands for managing them.<\/p>\n<h2 class=\"wp-block-heading\" id=\"aaba\">Problem<\/h2>\n<p class=\"wp-block-paragraph\" id=\"a597\">Imagine you have two Python projects on your laptop, each located in a different directory. You realize that you need to install the latest version of library A for the first project. Later, you switch to the second project and attempt to install library B.<\/p>\n<p class=\"wp-block-paragraph\" id=\"d8e4\">Here\u2019s the problem:\u00a0<em>library B depends on library A, but it requires a different version than the one you installed earlier<\/em>.<\/p>\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" data-dominant-color=\"f3e7da\" data-has-transparency=\"true\" style=\"--dominant-color: #f3e7da;\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"533\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/03\/Dependency-management-1-1024x533.png?resize=1024%2C533&#038;ssl=1\" alt=\"\" class=\"wp-image-599204 has-transparency\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/03\/Dependency-management-1-1024x533.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/03\/Dependency-management-1-300x156.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/03\/Dependency-management-1-768x400.png 768w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/03\/Dependency-management-1.png 1400w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\"><\/figure>\n<p class=\"wp-block-paragraph\" id=\"e4bb\">Since you haven\u2019t used any tool for <a href=\"https:\/\/towardsdatascience.com\/tag\/dependency-management\/\" title=\"Dependency Management\">Dependency Management<\/a>, all dependencies are installed globally on your computer. Due to the incompatible versions of library A, you encounter an error when trying to install library B.<\/p>\n<h2 class=\"wp-block-heading\" id=\"5ca0\">Solution<\/h2>\n<p class=\"wp-block-paragraph\" id=\"25b9\">To prevent such issues, virtual environments are used. The idea is to allocate a separate storage space for each <a href=\"https:\/\/towardsdatascience.com\/tag\/python\/\" title=\"Python\">Python<\/a> project. Each storage will contain all the externally downloaded dependencies for a specific project in an isolated manner.<\/p>\n<p class=\"wp-block-paragraph\" id=\"bf30\">More specifically, if we download the same library A for two projects within their own virtual environments, library A will be downloaded twice \u2014 once for each environment. Moreover, the versions of the library can differ between the environments because each environment is completely isolated and does not interact with the others.<\/p>\n<p class=\"wp-block-paragraph\" id=\"fdb1\">Now that the motivation behind using virtual environments is clear, let\u2019s explore how to create them in Python.<\/p>\n<h2 class=\"wp-block-heading\" id=\"456d\">Virtual environments in Python<\/h2>\n<p class=\"wp-block-paragraph\" id=\"3075\">It is recommended to create a virtual environment in the root directory of a project. An environment is created using the following command in the terminal:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">python -m venv &lt;environment_name&gt;<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"b204\">By convention,\u00a0<em>&lt;environment_name&gt;<\/em>\u00a0is usually named\u00a0<strong>venv<\/strong>, so the command becomes:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">python -m venv venv<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"d610\">As a result, this command creates a directory called\u00a0<em>venv<\/em>, which contains the virtual environment itself. It is even possible to go inside that directory, but in most cases, it is not very useful, as the<em>\u00a0venv<\/em>\u00a0directory primarily contains system scripts that are not intended to be used directly.<\/p>\n<p class=\"wp-block-paragraph\" id=\"9877\">To activate the virtual environment, use the following command:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">source venv\/bin\/activate<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"c23f\">Once the environment is activated, we can install dependencies for the project. As long as the<em>\u00a0venv<\/em>\u00a0is activated, any installed dependency will only belong to that environment.<\/p>\n<p class=\"wp-block-paragraph\" id=\"4269\">To deactivate the virtual environment, type:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">deactivate<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Once the environment is deactivated, the terminal returns to its normal state. For example, you can switch to another project and activate its environment there.<\/p>\n<h2 class=\"wp-block-heading\" id=\"d964\">Dependency management<\/h2>\n<h3 class=\"wp-block-heading\" id=\"b152\">Installing libraries<\/h3>\n<p class=\"wp-block-paragraph\" id=\"bf38\">Before installing any dependencies, it is recommended to activate a virtual environment to ensure that installed libraries belong to a single project. This helps avoid global version conflicts.<\/p>\n<p class=\"wp-block-paragraph\" id=\"4b0a\">The most frequently used command for dependency management is pip. Compared to other alternatives,\u00a0<em>pip<\/em>\u00a0is intuitive and simple to use.<\/p>\n<p class=\"wp-block-paragraph\" id=\"8057\">To install a library, type:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">pip install &lt;library_name&gt;<\/code><\/pre>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\" id=\"ef17\"><em>In the examples below instead of the &lt;library_name&gt;, I will write pandas (the most commonly used data analysis library).<\/em><\/p>\n<\/blockquote>\n<p class=\"wp-block-paragraph\" id=\"c426\">So, for instance, if we wanted to download the latest version of pandas, we should have typed:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">pip install pandas<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"2b24\">In some scenarios, we might need to install a specific version of a library.\u00a0<em>pip<\/em>\u00a0provides a simple syntax to do that:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">pip install pandas==2.1.4 # install pandas of version 2.1.4\npip install pandas&gt;=2.1.4 # install pandas of version 2.1.4 or higher\npip install pandas&lt;2.1.4 # install pandas of version less than 2.1.4\npip install pandas&gt;=2.1.2,&lt;2.2.4 # installs the latest version available between 2.1.2 and 2.2.4 <\/code><\/pre>\n<h3 class=\"wp-block-heading\" id=\"5958\">Viewing dependency details<\/h3>\n<p class=\"wp-block-paragraph\" id=\"026a\">If you are interested in a particular dependency that you have installed, a simple way to get more information about it is to use the <code>pip show<\/code> command:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">pip show pandas<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"fff4\">For example, the command in the example will output the following information:<\/p>\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" data-dominant-color=\"f2f2f2\" data-has-transparency=\"true\" style=\"--dominant-color: #f2f2f2;\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"320\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/03\/Dependency-management-2-1024x320.png?resize=1024%2C320&#038;ssl=1\" alt=\"\" class=\"wp-image-599205 has-transparency\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/03\/Dependency-management-2-1024x320.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/03\/Dependency-management-2-300x94.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/03\/Dependency-management-2-768x240.png 768w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/03\/Dependency-management-2.png 1400w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\"><figcaption class=\"wp-element-caption\">Example of the output of the\u00a0<strong>pip show<\/strong>\u00a0command<\/figcaption><\/figure>\n<h3 class=\"wp-block-heading\" id=\"acad\">Deleting dependency<\/h3>\n<p class=\"wp-block-paragraph\" id=\"baf1\">To remove a dependency from a virtual environment, use the following command:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">pip uninstall pandas<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"b54a\">After executing this command, all files related to the specified library will be deleted, thus freeing up disk space. However, if you run a Python program that imports this library again, you will encounter an ImportError.<\/p>\n<h2 class=\"wp-block-heading\" id=\"7051\">File with requirements<\/h2>\n<p class=\"wp-block-paragraph\" id=\"b222\">A common practice when managing dependencies is to create a\u00a0<em>requirements.txt<\/em>\u00a0file that contains a list of all downloaded dependencies in the project along with their versions. Here is an example of what it might look like:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-markup\">fastapi==0.115.5\npydantic==2.10.1\nPyYAML==6.0.2\nrequests==2.32.3\nscikit-learn==1.5.2\nscipy==1.14.1\nseaborn==0.13.2\nstreamlit==1.40.2\ntorch==2.5.1\ntorchvision==0.20.1\ntornado==6.4.2\ntqdm==4.67.1\nurllib3==2.2.3\nuvicorn==0.32.1\nyolo==0.3.2<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"555e\">Ideally, every time you use the <code>pip install<\/code> command, you should add a corresponding line to the\u00a0<em>requirements.txt<\/em>\u00a0file to keep track of all the libraries used in the project.<\/p>\n<p class=\"wp-block-paragraph\" id=\"131e\">However, if you forget to do that, there is still an alternative: the\u00a0<code>pip freeze<\/code> command outputs all of the installed dependencies in the project. Nevertheless,\u00a0<code>pip freeze<\/code>\u00a0can be quite verbose, often including many other library names that are dependencies of the libraries you are using in the project.<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">pip freeze &gt; requirements.txt<\/code><\/pre>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\" id=\"28a9\"><em>Given this, it\u2019s a good habit to add installed requirements with their versions to the requirements.txt file.<\/em><\/p>\n<\/blockquote>\n<p class=\"wp-block-paragraph\" id=\"f597\">Whenever you clone a Python project, it is expected that a requirements.txt file is already present in the Git repository. To install all the dependencies listed in this file, you use the <code>pip install<\/code> command along with the -r flag followed by the requirements filename.<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">pip install -r requirements.txt<\/code><\/pre>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\" id=\"24d3\"><em>Conversely, whenever you work on a Python project, you should create a requirements.txt file so that other collaborators can easily install the necessary dependencies.<\/em><\/p>\n<\/blockquote>\n<h2 class=\"wp-block-heading\" id=\"2e83\">.gitignore<\/h2>\n<p class=\"wp-block-paragraph\" id=\"ee67\">When working with version control systems, virtual environments should never be pushed to Git! Instead, they must be mentioned in a .gitignore file.<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\" id=\"db4e\"><em>Virtual environments tend to be very large, and if there is an existing requirements.txt file, there should be no problem downloading all necessary dependencies.<\/em><\/p>\n<\/blockquote>\n<h2 class=\"wp-block-heading\" id=\"53ae\">Conclusion<\/h2>\n<p class=\"wp-block-paragraph\" id=\"3635\">In this article, we have looked at the very important concept of virtual environments. By isolating downloaded dependencies for different projects, they allow for easier management of multiple <a href=\"https:\/\/towardsdatascience.com\/tag\/python-projects\/\" title=\"Python Projects\">Python Projects<\/a>.<\/p>\n<p class=\"wp-block-paragraph\"><em>All images are by the author unless noted otherwise.<\/em><\/p>\n<p>The post <a href=\"https:\/\/towardsdatascience.com\/comprehensive-guide-to-dependency-management-in-python\/\">Comprehensive Guide to Dependency Management in Python<\/a> appeared first on <a href=\"https:\/\/towardsdatascience.com\/\">Towards Data Science<\/a>.<\/p>\n<\/div>\n<p> \t<BR><br \/>\n <BR><\/BR><br \/>\n    Vyacheslav Efimov<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n<a href=\"https:\/\/towardsdatascience.com\/comprehensive-guide-to-dependency-management-in-python\/\">Go to original source<\/a><br \/>\n \t<BR><br \/>\n <BR><\/BR><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Comprehensive Guide to Dependency Management in Python Introduction When learning Python, many beginners focus solely on the language and its libraries while completely ignoring virtual environments. As a result, managing Python projects can become a mess: dependencies installed for different projects may have conflicting versions, leading to compatibility issues. Even when I studied Python, nobody [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,1968,160,157,1969,158,1970],"tags":[791,102,1971],"class_list":["post-2289","post","type-post","status-publish","format-standard","hentry","category-aimldsaimlds","category-dependency-management","category-programming","category-python","category-python-projects","category-tips-and-tricks","category-virtual-environment","tag-library","tag-python","tag-virtual"],"_links":{"self":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/2289"}],"collection":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/comments?post=2289"}],"version-history":[{"count":0,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/2289\/revisions"}],"wp:attachment":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/media?parent=2289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/categories?post=2289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/tags?post=2289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}