{"id":175,"date":"2020-12-13T16:06:10","date_gmt":"2020-12-13T16:06:10","guid":{"rendered":"https:\/\/web.archive.org\/web\/20210304030817\/https:\/\/beta.python3.guide\/?page_id=175"},"modified":"2021-02-27T10:41:21","modified_gmt":"2021-02-27T09:41:21","slug":"virtual-environments","status":"publish","type":"post","link":"https:\/\/web.archive.org\/web\/20210304030817\/https:\/\/python.land\/virtual-environments","title":{"rendered":"Virtual Environments"},"content":{"rendered":"\n<p>Python virtual environments allow you to install Python packages and a Python interpreter in an isolated location from the rest of your system, instead of installing them globally. This has a few advantages that we&#8217;ll go over first.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_16 counter-hierarchy ez-toc-grey\">\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/python.land\/virtual-environments\/#Why_you_need_virtual_environments\" title=\"Why you need virtual environments\">Why you need virtual environments<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/python.land\/virtual-environments\/#Preventing_version_conflicts\" title=\"Preventing version conflicts\">Preventing version conflicts<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/python.land\/virtual-environments\/#Easy_to_reproduce_and_install\" title=\"Easy to reproduce and install\">Easy to reproduce and install<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/python.land\/virtual-environments\/#Works_everywhere_even_when_not_root\" title=\"Works everywhere, even when not root\">Works everywhere, even when not root<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/python.land\/virtual-environments\/#Python_Virtual_Environments_vs_other_options\" title=\"Python Virtual Environments vs. other options\">Python Virtual Environments vs. other options<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n\n<h2><span class=\"ez-toc-section\" id=\"Why_you_need_virtual_environments\"><\/span>Why you need virtual environments<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3><span class=\"ez-toc-section\" id=\"Preventing_version_conflicts\"><\/span>Preventing version conflicts<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you install third-party packages system-wide, you could argue that you\u2019re very efficient. After all, you only need to install it once, saving you precious time and disk space. There\u2019s a problem with this approach that may start to unfold weeks or months later, however.<\/p>\n\n\n\n<p>Suppose your project,&nbsp;<code>Project A<\/code>, is written against a specific version of&nbsp;<code>library X<\/code>. In the future, you might need to upgrade&nbsp;library X. Say, for example, you need the latest version for another project you started, called&nbsp;Project B. You upgrade&nbsp;library X&nbsp;to the latest version, and&nbsp;project B&nbsp;starts working fine. Great! But once you did this, it turns out your&nbsp;<code>Project A<\/code>&nbsp;code broke badly. After all, APIs can be completely different between major version upgrades.<\/p>\n\n\n\n<p>A virtual environment fixes this problem by isolating your project from other projects and system-wide packages.<\/p>\n\n\n\n<h3><span class=\"ez-toc-section\" id=\"Easy_to_reproduce_and_install\"><\/span>Easy to reproduce and install<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Virtual environments make it easy to define and install the packages specific to your project. Additionally, you can define exact version numbers for the required packages to make sure your project will work with a version that has been tested with your code. This also helps other users of your software since a virtual environment will help others reproduce the exact environment for which your software was built.<\/p>\n\n\n\n<h3><span class=\"ez-toc-section\" id=\"Works_everywhere_even_when_not_root\"><\/span>Works everywhere, even when not root<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you\u2019re working on a shared host, like those at a university or a web hosting provider, you won\u2019t be able to install packages globally since you don\u2019t have the administrator rights to do so. In these places, a virtual environment allows you to install anything you want locally.<\/p>\n\n\n\n<h2><span class=\"ez-toc-section\" id=\"Python_Virtual_Environments_vs_other_options\"><\/span>Python Virtual Environments vs. other options<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are other options to isolate your project from others.<\/p>\n\n\n\n<ol><li>In the most extreme case, you could buy a second PC and run your code there. Problem fixed! It\u2019s a bit expensive, though!<\/li><li>A virtual machine is a much cheaper option but still requires you to install a complete operating system. A bit of a waste as well, right?<\/li><li>Next in line is&nbsp;<a href=\"https:\/\/python.land\/deployment\/containerize-your-project\">containerization, with the likes of Docker and Kubernetes<\/a>. These can be very powerful, but it&#8217;s still a bit too much and too complicated for a small Python project.<\/li><\/ol>\n\n\n\n<p>And that\u2019s where virtual environments comes in!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why you need virtual environmentsPreventing version conflictsEasy to reproduce and installWorks everywhere, even when not rootPython Virtual Environments vs. other options Python virtual environments allow &#8230; <a title=\"Virtual Environments\" class=\"read-more\" href=\"https:\/\/python.land\/virtual-environments\" aria-label=\"More on Virtual Environments\">read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[30],"tags":[29],"modified_by":"Erik van Baaren","menu_order":70,"_links":{"self":[{"href":"https:\/\/web.archive.org\/web\/20210304030817\/https:\/\/python.land\/wp-json\/wp\/v2\/posts\/175"}],"collection":[{"href":"https:\/\/web.archive.org\/web\/20210304030817\/https:\/\/python.land\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/web.archive.org\/web\/20210304030817\/https:\/\/python.land\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/web.archive.org\/web\/20210304030817\/https:\/\/python.land\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/web.archive.org\/web\/20210304030817\/https:\/\/python.land\/wp-json\/wp\/v2\/comments?post=175"}],"version-history":[{"count":1,"href":"https:\/\/web.archive.org\/web\/20210304030817\/https:\/\/python.land\/wp-json\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":1977,"href":"https:\/\/web.archive.org\/web\/20210304030817\/https:\/\/python.land\/wp-json\/wp\/v2\/posts\/175\/revisions\/1977"}],"wp:attachment":[{"href":"https:\/\/web.archive.org\/web\/20210304030817\/https:\/\/python.land\/wp-json\/wp\/v2\/media?parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/web.archive.org\/web\/20210304030817\/https:\/\/python.land\/wp-json\/wp\/v2\/categories?post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/web.archive.org\/web\/20210304030817\/https:\/\/python.land\/wp-json\/wp\/v2\/tags?post=175"}],"curies":[{"name":"wp","href":"https:\/\/web.archive.org\/web\/20210304030817\/https:\/\/api.w.org\/{rel}","templated":true}]}}