{"id":86938,"date":"2023-02-17T11:30:39","date_gmt":"2023-02-17T06:00:39","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=86938"},"modified":"2023-02-17T11:30:39","modified_gmt":"2023-02-17T06:00:39","slug":"python-virtual-environment","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/","title":{"rendered":"Python Virtual Environment"},"content":{"rendered":"<p><span style=\"font-weight: 400\">Packages and modules not found in the standard library are often used in Python applications. Applications will occasionally require a specific version of a library because they may have used an outdated version of the interface when developing them or because they need to solve a particular issue.<\/span><\/p>\n<p><span style=\"font-weight: 400\">This implies that it might not be possible for a single Python installation to satisfy all of an application&#8217;s requirements. When two applications have conflicting requirements, such as when program A requires version 1.0 of a module but application B requires version 2.0, neither version can be installed without the other application being unable to function.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Making a virtual environment\u2014a self-contained directory tree including a Python installation for a certain version of Python and several additional packages\u2014is the answer to this issue.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Then, various apps can utilize various virtual environments. Application A can have its virtual environment with version 1.0 installed while Application B has a different virtual environment with version 2.0 installed to resolve the preceding example of conflicting requirements. Application A&#8217;s environment won&#8217;t be impacted if Application B needs a library to be upgraded to version 3.0. Let us learn more about Python virtual environment.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">What is Virtualenv?<\/span><\/h3>\n<p><span style=\"font-weight: 400\">A virtual environment is a Python utility for dependency management and project isolation. Instead of being deployed worldwide, they enable the installation of Python site-packages (third-party libraries) locally in an isolated directory for a specific project (i.e. as part of a system-wide Python).<\/span><\/p>\n<p><span style=\"font-weight: 400\">All of that is fantastic, but what exactly is a virtual environment? A directory with three essential components is all that a virtual environment is:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">an installation location for third-party libraries called <\/span><span style=\"font-weight: 400\">site packages.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Symlinks to Python programs that are already installed on your computer.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">They make it so that programmers who use Python can write programs using Python.<\/span><\/li>\n<\/ul>\n<h3><span style=\"font-weight: 400\">Why Use Virtual Environment?<\/span><\/h3>\n<p><span style=\"font-weight: 400\">Virtual environments make it simple to resolve a wide range of potential problems. In particular, they help you with<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Identifying which versions of a package are suitable for usage with various projects may help to resolve dependency problems. For Project X, you may use Package A v2.7, and for Project Y, Package A v1.3.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">You may make a project independent and repeatable by listing each package&#8217;s dependencies in a requirements file.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Install packages on a server where you don&#8217;t have administrative privileges.<\/span><\/li>\n<li><span style=\"font-weight: 400\">By not having to install packages that you might only use for one project across the board, you can keep your global<\/span><span style=\"font-weight: 400\"> site packages\/<\/span><span style=\"font-weight: 400\"> directory organized.<\/span><\/li>\n<\/ul>\n<h3><span style=\"font-weight: 400\">How Does a Python Virtual Environment Work?<\/span><\/h3>\n<p><span style=\"font-weight: 400\">Venv is the name of the module that is used to build and control virtual environments. Normally, venv will install the most recent version of Python that is accessible to you. If your computer has numerous versions of Python installed, you can choose one by typing python3 or your preferred version<\/span><\/p>\n<p><span style=\"font-weight: 400\">The venv module of Python is used in the steps of this tutorial to build virtual environments. Since Python 3.5, using this standard library module to build virtual environments is the accepted best practice.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Venv is a great option for simple usage because it is pre-packaged with your Python installation. With that in mind, you&#8217;re prepared to start this tutorial&#8217;s initial virtual environment creation.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">Creating Virtual Environment in Python<\/span><\/h3>\n<p><span style=\"font-weight: 400\">It&#8217;s best to first construct a virtual environment whenever you&#8217;re working on a Python project that utilizes external dependencies that you&#8217;re installing with pip:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">PS&gt; python -m venv venv<\/pre>\n<p><span style=\"font-weight: 400\">You may need to specify the complete path to your Python executable if you&#8217;re using Python on Windows and haven&#8217;t set the<\/span> <span style=\"font-weight: 400\">PATH<\/span> <span style=\"font-weight: 400\">and <\/span><span style=\"font-weight: 400\">PATHEXT <\/span><span style=\"font-weight: 400\">variables:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">PS&gt; C:\\Users\\TechVidvan\\AppData\\Local\\Programs\\Python\\Python310\\python -m venv venv<\/pre>\n<p><span style=\"font-weight: 400\">The system path displayed above presupposes that you used the Python downloads page&#8217;s Windows installer to set up Python 3.10 on your computer. Your system&#8217;s Python executable may be located in a different location. The where.exe python command can be used to locate the path while working with PowerShell.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">Activating Virtual Environment in Python<\/span><\/h3>\n<p><span style=\"font-weight: 400\">Your project now has a unique virtual setting. Typically, you&#8217;ll activate the environment by running a script that is included with the installation before you begin using it:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">PS&gt; venv\\Scripts\\activate\n(venv) PS&gt;\n<\/pre>\n<p><span style=\"font-weight: 400\">Make sure you are in the folder containing the virtual environment you have established before executing this command.<\/span><\/p>\n<p><span style=\"font-weight: 400\">When the name of your virtual environment\u2014in this case, (venv)\u2014is visible in your command prompt, your virtual environment is operational. You&#8217;re prepared to install your external packages at this point.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">Installing Virtual Environment in Python<\/span><\/h3>\n<p><span style=\"font-weight: 400\">Your virtual environment is now ready for use, and you can add any external dependencies your project requires:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">(venv) PS&gt; python -m pip install &lt;package-name&gt;\n<\/pre>\n<p><span style=\"font-weight: 400\">The default command to use when installing external Python packages through pip is this one. Because the virtual environment was first established and turned on, pip will set up the packages in a separate location.<\/span><\/p>\n<p><span style=\"font-weight: 400\">The installation of your packages into your virtual environment is now possible. You started by making a Python virtual environment called venv and activating it in the current shell session to get to this stage.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Every Python package you install will end up in this contained environment rather than your global Python <\/span><span style=\"font-weight: 400\">site packages<\/span><span style=\"font-weight: 400\"> as long as you don&#8217;t close your terminal. As a result, you may now work on your Python project without being concerned about any dependency issues.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">Deactivating Python Virtual Environment<\/span><\/h3>\n<p><span style=\"font-weight: 400\">You can deactivate this virtual environment when you&#8217;re done with it:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">(venv) PS&gt; deactivate\nPS&gt;\n<\/pre>\n<p><span style=\"font-weight: 400\">The deactivate command causes your command prompt to reset to its default state. This modification indicates that you have left your virtual world. You will interact with your locally configured Python environment if you use Python or pip right now.<\/span><\/p>\n<p><span style=\"font-weight: 400\">You must once again run the virtual environment&#8217;s activate script if you want to enter a previous virtual environment that you&#8217;ve built.<\/span><\/p>\n<p><span style=\"font-weight: 400\">You&#8217;ve now studied the fundamentals of using Python virtual environments. If that&#8217;s all you require, good luck with your future creations!<\/span><\/p>\n<p><span style=\"font-weight: 400\">You should continue reading if you want to learn more about what happened, What a Python virtual environment is, why so many tutorials demand that you create one, and why this is necessary. You&#8217;re going deep soon!<\/span><\/p>\n<h3><span style=\"font-weight: 400\">Managing Environment in Python<\/span><\/h3>\n<p><span style=\"font-weight: 400\">Requirements Files:<\/span><\/p>\n<p><span style=\"font-weight: 400\">The simplest way to enable people to reproduce our work is to put a requirements file in the root directory of our project (top directory). <\/span><span style=\"font-weight: 400\">Pip freeze<\/span><span style=\"font-weight: 400\">, which displays a list of installed third-party packages along with their version numbers, will be used to accomplish this.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">(venv) % pip freeze\nnumpy==1.15.3 \n<\/pre>\n<p><span style=\"font-weight: 400\">Likewise, create a file we&#8217;ll name requirements.txt with the output in it. <\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">(venv) % pip freeze &gt; requirements.txt\n<\/pre>\n<p><span style=\"font-weight: 400\">When we update or install a new package, we may use the same command to update our needs file.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Anyone we share our project with can now use our requirements.txt file to duplicate our environment and run our project on their PC.<\/span><\/p>\n<h3><span style=\"font-weight: 400\">Conclusion<\/span><\/h3>\n<p><span style=\"font-weight: 400\">You developed a solid grasp of python virtual environment throughout the lesson, including what they are, why you need them, how they work internally, and how to manage them on your system.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Packages and modules not found in the standard library are often used in Python applications. Applications will occasionally require a specific version of a library because they may have used an outdated version of&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":87203,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[4848],"class_list":["post-86938","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python-virtual-environment"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Virtual Environment - TechVidvan<\/title>\n<meta name=\"description\" content=\"See python virtual environment, what they are, why you need them, how they work internally, and how to manage them on your system.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Virtual Environment - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"See python virtual environment, what they are, why you need them, how they work internally, and how to manage them on your system.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-17T06:00:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/02\/python-virtual-environments-and-packages.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Virtual Environment - TechVidvan","description":"See python virtual environment, what they are, why you need them, how they work internally, and how to manage them on your system.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/","og_locale":"en_US","og_type":"article","og_title":"Python Virtual Environment - TechVidvan","og_description":"See python virtual environment, what they are, why you need them, how they work internally, and how to manage them on your system.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2023-02-17T06:00:39+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/02\/python-virtual-environments-and-packages.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Virtual Environment","datePublished":"2023-02-17T06:00:39+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/"},"wordCount":1159,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/02\/python-virtual-environments-and-packages.webp","keywords":["Python Virtual Environment"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/","url":"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/","name":"Python Virtual Environment - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/02\/python-virtual-environments-and-packages.webp","datePublished":"2023-02-17T06:00:39+00:00","description":"See python virtual environment, what they are, why you need them, how they work internally, and how to manage them on your system.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/02\/python-virtual-environments-and-packages.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2023\/02\/python-virtual-environments-and-packages.webp","width":1200,"height":628,"caption":"python virtual environments and packages"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-virtual-environment\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Virtual Environment"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/86938","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=86938"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/86938\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/87203"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=86938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=86938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=86938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}