{"id":6920,"date":"2024-04-11T11:58:32","date_gmt":"2024-04-11T11:58:32","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=6920"},"modified":"2024-04-11T11:58:32","modified_gmt":"2024-04-11T11:58:32","slug":"python-virtual-environment","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-virtual-environment\/","title":{"rendered":"What is a Python-Virtual Environment?"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-python-virtual-environment\">What is Python Virtual Environment?<\/a><\/li><li ><a href=\"#how-to-use-a-virtual-environment-for-python-application\">How to Use a Virtual Environment for Python Application?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-virtual-environment-fa-qs\"> Python-Virtual Environment- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>Python virtual environment will make a virtual installation of Python inside the <a href=\"https:\/\/www.skillvertex.com\/blog\/directory-submission-sites\/\" data-type=\"post\" data-id=\"763\">Python directory<\/a>. Then, users will install and monitor Python Packages from several other projects.  This article will tell you about the virtual environment in Python.<\/p>\n\n\n\n<p>Moreover, it will help the users install packages and change the Python environment without breaking off the packages that are installed in the other environment.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-virtual-environment\">What is Python Virtual Environment?<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.skillvertex.com\/blog\/features-of-python\/\" data-type=\"post\" data-id=\"6868\">Python<\/a> virtual environments are :<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Disposable Nature:<\/strong> Virtual environments are like temporary workspaces for your<a href=\"https:\/\/www.skillvertex.com\/blog\/data-science-project-ideas\/\" data-type=\"post\" data-id=\"5596\"> Python projects<\/a>. Once you&#8217;re done with a project, you can remove this workspace without affecting others.<\/li>\n\n\n\n<li><strong>Purpose:<\/strong> Each virtual environment is a self-contained package that holds a specific <a href=\"https:\/\/www.skillvertex.com\/blog\/python-history\/\" data-type=\"post\" data-id=\"6864\">Python version<\/a> and the exact tools your project needs. This avoids conflicts between different projects using different tools.<\/li>\n\n\n\n<li><strong>Directory Convention:<\/strong> Virtual environments are typically stored in a folder named &#8220;venv&#8221; or &#8220;.venv&#8221; within your project directory. This keeps things organized and helps Python recognize these special environments.<\/li>\n\n\n\n<li><strong>Immovable\/Copyable:<\/strong> While virtual environments are portable, it&#8217;s not recommended to directly copy them or move them between projects. It&#8217;s better to recreate the environment based on the project&#8217;s requirements.<\/li>\n<\/ol>\n\n\n\n<p>Creating a virtual environment is like setting up a controlled environment for your project, ensuring that the project has all the necessary tools without interfering with other projects. It&#8217;s a way of maintaining order in the Python ecosystem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-use-a-virtual-environment-for-python-application\">How to Use a Virtual Environment for Python Application?<\/h2>\n\n\n\n<p>After installing Python<a href=\"https:\/\/www.skillvertex.com\/blog\/software-engineer-salary\/\" data-type=\"post\" data-id=\"445\"> software <\/a>on your computer and will be accessible from anywhere in the system. It is a system-wide installation.<\/p>\n\n\n\n<p>During the creation of the application in Python, more libraries are needed to be installed with the pip utility. Further, the application must need a particular version of the <a href=\"https:\/\/www.skillvertex.com\/blog\/c-library-math-h-functions\/\" data-type=\"post\" data-id=\"2596\">library<\/a>.  Similarly,  another Python <a href=\"https:\/\/www.skillvertex.com\/blog\/which-of-the-following-is-the-central-application-in-the-aws-portfolio\/\" data-type=\"post\" data-id=\"2719\">application <\/a>( example: App2) needs a newer version of the same library so, let&#8217;s suppose some lib 2.0. Therefore, installing this new version can allow the functionality to be compromised due to the conflict between two different versions of the same library.<\/p>\n\n\n\n<p>Further, this conflict will be neglected by allowing the two isolated environments of Python on the same machine. It is referred as a virtual machine.  A <a href=\"https:\/\/www.skillvertex.com\/blog\/setting-up-virtual-environment-in-python\/\" data-type=\"post\" data-id=\"6906\">virtual environment<\/a> is considered a separate directory structure and has an isolated installation with a local copy of Python Interpreter, a standard library, and several other modules.<\/p>\n\n\n\n<p>With the global Python installation, it is possible to make more than one virtual environment.  Each virtual environment has a different version of the same library. Thus, this can help to avoid the conflict.<\/p>\n\n\n\n<p>This functionality will be supported by the venv module in the standard <a href=\"https:\/\/www.skillvertex.com\/blog\/python-tutorial\/\" data-type=\"post\" data-id=\"6852\">Python<\/a> distribution. To create a new virtual environment, use the command given below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>C:\\Users\\Acer&gt;md\\pythonapp\nC:\\Users\\Acer&gt;cd\\pythonapp\nC:\\pythonapp&gt;python -m venv myvenv<\/code><\/pre>\n\n\n\n<p>myvenv is considered as the folder that has a new Python Virtual environment  and can make  the directory structure given below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Directory of C:\\pythonapp\\myvenv\n22-02-2023 09:53 &lt;DIR&gt; .\n22-02-2023 09:53 &lt;DIR&gt; ..\n22-02-2023 09:53 &lt;DIR&gt; Include\n22-02-2023 09:53 &lt;DIR&gt; Lib\n22-02-2023 09:53 77 pyvenv.cfg\n22-02-2023 09:53 &lt;DIR&gt; Scripts<\/code><\/pre>\n\n\n\n<p>Therefore, the utilities that are used for activating and deactivating the virtual environment and the local copy of the Python interpreter will be stored in the scripts folder.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Directory of C:\\pythonapp\\myvenv\\scripts\n22-02-2023 09:53 &lt;DIR&gt; .\n22-02-2023 09:53 &lt;DIR&gt; ..\n22-02-2023 09:53 2,063 activate\n22-02-2023 09:53 992 activate.bat\n22-02-2023 09:53 19,611 Activate.ps1\n22-02-2023 09:53 393 deactivate.bat\n22-02-2023 09:53 106,349 pip.exe\n22-02-2023 09:53 106,349 pip3.10.exe\n22-02-2023 09:53 106,349 pip3.exe\n22-02-2023 09:53 242,408 python.exe\n22-02-2023 09:53 232,688 pythonw.exe<\/code><\/pre>\n\n\n\n<p>Execute the <strong>activate.bat<\/strong> in the Scripts folder to allow the new virtual environment.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>C:\\pythonapp&gt;myvenv\\scripts\\activate\n(myvenv) C:\\pythonapp&gt;<\/code><\/pre>\n\n\n\n<p>Remember to note the name of the virtual environment in the parenthesis. Whereas, the script folder has a local copy of Python Interpreter. Then, start a Python Session in the virtual environment. Check the sys. path to confirm if the Python session is in the virtual environment.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(myvenv) C:\\pythonapp&gt;python\nPython 3.10.1 (tags\/v3.10.1:2cd268a, Dec 6 2021, 19:10:37) &#91;MSC v.1929\n64 bit (AMD64)] on win32\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n&gt;&gt;&gt; import sys\n&gt;&gt;&gt; sys.path\n&#91;'', 'C:\\\\Python310\\\\python310.zip', 'C:\\\\Python310\\\\DLLs',\n'C:\\\\Python310\\\\lib', 'C:\\\\Python310', 'C:\\\\pythonapp\\\\myvenv',\n'C:\\\\pythonapp\\\\myvenv\\\\lib\\\\site-packages']\n&gt;&gt;&gt;<\/code><\/pre>\n\n\n\n<p>Hence, the script folder of the virtual environment has pip utilities. Therefore, after installing a package from PyPI, the package will be seen as active only in the virtual environment. Run <strong>deactivate.bat<\/strong> to deactivate the environment.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>To conclude, a Python virtual environment is like a special workspace or toolbox for each of your projects. It helps keep everything tidy and separate, so tools from one project don&#8217;t mix up with tools from another. It&#8217;s like having individual backpacks for different school subjects. <\/p>\n\n\n\n<p>Furthermore, it creates a new one whenever you start a project, and when you&#8217;re done, you can easily toss it away. This way, your projects stay organized, and you always have the right tools for the job without any confusion.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-virtual-environment-fa-qs\"> Python-Virtual Environment- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1706790702588\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. Which virtual environment is best for Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <strong>Python venv <\/strong>is the virtual environment that is best for Python.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1706790711551\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.What are the two advantages of Python virtual environments?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Python virtual environments will allow developers to monitor software dependencies in Python code. <\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1706790720473\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.  Should I use Anaconda or venv?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <strong>Venv might be the best choice<\/strong> as it is very simple and easy to use.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Python virtual environment will make a virtual installation of Python inside the Python directory. Then, users will install and monitor Python Packages from several other projects. This article will tell you about the virtual environment in Python. Moreover, it will help the users install packages and change the Python environment without breaking off the packages &#8230; <a title=\"What is a Python-Virtual Environment?\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-virtual-environment\/\" aria-label=\"More on What is a Python-Virtual Environment?\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":6921,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[57,879],"class_list":["post-6920","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-python","tag-python-virtual-environment","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6920","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=6920"}],"version-history":[{"count":7,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6920\/revisions"}],"predecessor-version":[{"id":8893,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/6920\/revisions\/8893"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/6921"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=6920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=6920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=6920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}