{"id":69,"date":"2022-04-15T22:56:59","date_gmt":"2022-04-15T22:56:59","guid":{"rendered":"https:\/\/codecrypt76.com\/?p=69"},"modified":"2022-11-25T16:52:11","modified_gmt":"2022-11-25T16:52:11","slug":"virtual-environments-using-venv-library","status":"publish","type":"post","link":"https:\/\/codecrypt76.com\/index.php\/2022\/04\/15\/virtual-environments-using-venv-library\/","title":{"rendered":"Virtual Environments Using venv Library"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" width=\"750\" height=\"423\" src=\"http:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/04\/virtual-reality-thumbnail-1.png\" alt=\"Virtual Environments\" class=\"wp-image-71\" srcset=\"https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/04\/virtual-reality-thumbnail-1.png 750w, https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/04\/virtual-reality-thumbnail-1-300x169.png 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h2>&nbsp;<\/h2>\n\n\n\n<p>Imagine a scenario where you are working on two projects, and one project uses Django 2.2, and the other uses Django 3.2. This would be a real problem since Python can\u2019t differentiate between versions in the&nbsp;site-packages&nbsp;directory. Both version of Django would reside in the same directory with the same name. Since there is no differentiation between versions, both projects would be required to use the same version, which is unacceptable in this case. This is where virtual environments tools come into play.<\/p>\n\n\n\n<p>The main purpose of Python virtual environments is to create an isolated environment for Python projects. Each project can have its own dependencies, regardless of what dependencies every other project has. In addition, a virtual environment is also useful when you need to work on a shared system and do not have permission to install packages as you will be able to install them in the virtual environment.<\/p>\n\n\n\n<h2>What is Venv?<\/h2>\n\n\n\n<p>The venv module is used to manage Python packages for different projects. venv is a package shipped with Python 3, therefore, you do not have to install it. Using venv allows you to avoid installing Python packages globally which could break system tools or other projects. It provides support for creating lightweight \u201cvirtual environments\u201d with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories<\/p>\n\n\n\n<p>The&nbsp;<a href=\"https:\/\/docs.python.org\/3\/library\/venv.html#module-venv\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/venv.html#module-venv\" target=\"_blank\" rel=\"noreferrer noopener\">Venv<\/a>&nbsp;is a module shipped with Python 3.3+.&nbsp; It provides support for creating lightweight \u201cvirtual environments\u201d with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories. It serves the same purpose as&nbsp;virtualenv; however, virtualenv continues to be more popular than venv, especially since the former supports both Python 2 and 3. However, it\u2019s really a matter of personal preference. <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/venv.html#module-venv\" data-type=\"URL\" data-id=\"https:\/\/docs.python.org\/3\/library\/venv.html#module-venv\" target=\"_blank\">Venv<\/a>&nbsp;&nbsp;to be the most hassle-free of the options because it&#8217;s built into Python, and just works out of the box. However different people have different preferences and use-cases so considerations may vary. Another very popular Dependency Management tool is <a rel=\"noreferrer noopener\" href=\"https:\/\/pypi.org\/project\/poetry\/\" target=\"_blank\">poetry<\/a> which I plan to explore soon.<\/p>\n\n\n\n<h2>&nbsp;Creating virtual environments<\/h2>\n\n\n\n<p>Creation of&nbsp;<a href=\"https:\/\/docs.python.org\/3\/library\/venv.html#venv-def\" target=\"_blank\" rel=\"noreferrer noopener\">virtual environments<\/a>&nbsp;is done by executing the command&nbsp;venv:<\/p>\n\n\n\n<p>python -m venv path\/to\/new\/environment<\/p>\n\n\n\n<p>Running this command creates the target directory (creating any parent directories that don\u2019t exist already) and places a&nbsp;pyvenv.cfg&nbsp;file in it with a&nbsp;home&nbsp;key pointing to the Python installation from which the command was run (a common name for the target directory is&nbsp;.venv).&nbsp;<\/p>\n\n\n\n<h1>Objective<\/h1>\n\n\n\n<p>We are going to create a virtual folder for Matplotlib.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">project-1\/<br>\u251c\u2500\u2500 main.py<\/pre>\n\n\n\n<h2><a href=\"https:\/\/github.com\/seraph776\/virtaul-enviornment\/blob\/main\/README.md#step-1-open-git-bash-and-create-a-project-folder\"><\/a>Step-1: Open Git-Bash and create a project folder<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir project1<\/code><\/pre>\n\n\n\n<h2><a href=\"https:\/\/github.com\/seraph776\/virtaul-enviornment\/blob\/main\/README.md#step-2-change-directory-into-project-folder\"><\/a>Step-2: Change directory into project folder<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>cd project-1<\/code><\/pre>\n\n\n\n<h2><a href=\"https:\/\/github.com\/seraph776\/virtaul-enviornment\/blob\/main\/README.md#step-3-create-the-virtual-enviornment-folder\"><\/a>Step-3: Create the Virtual Enviornment Folder<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>python -m venv venv<\/code><\/pre>\n\n\n\n<p>See screenshot below to see a break-down of the COMMAND so you understand it:&nbsp;<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/user-images.githubusercontent.com\/72005563\/137311235-25c15bf3-c1e8-4911-80fd-1be742493f45.png\"><\/a><\/p>\n\n\n\n<p>The first command is&nbsp;<code>python<\/code>&nbsp;because it is a python command&nbsp;<code>-m<\/code>&nbsp;stand for module, the&nbsp;<code>venv<\/code>&nbsp;is the&nbsp;<code>module name<\/code>&nbsp;to be ran and the last&nbsp;<code>venv<\/code>&nbsp;is the name of the&nbsp;<code>Virtual Environment Folder<\/code>. You can name this ANYTHING you wish, however by convention it is named&nbsp;<code>venv<\/code>. After running this command, you can see a folder named&nbsp;<code>venv<\/code>&nbsp;was created inside&nbsp;<code>project-1<\/code>&nbsp;directory.&nbsp;<a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/user-images.githubusercontent.com\/72005563\/137312360-57a351f6-9f09-47ec-885c-340599e7bb3e.png\"><\/a><\/p>\n\n\n\n<h2><a href=\"https:\/\/github.com\/seraph776\/virtaul-enviornment\/blob\/main\/README.md#step-4-activate-virtual-enviornment\"><\/a>Step-4: Activate Virtual Environment<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>. venv\/scripts\/activate<\/code><\/pre><\/div>\n\n\n\n<p>Once the virtual enviornment has been activate your shell prompt will change. You will see your virtual environment folder name in parenthesis.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/user-images.githubusercontent.com\/72005563\/137311668-181930ba-5995-42ad-8514-02ea33c7e3b4.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img src=\"https:\/\/user-images.githubusercontent.com\/72005563\/137311668-181930ba-5995-42ad-8514-02ea33c7e3b4.png\" alt=\"virtual environment activated\"\/><\/a><\/figure>\n\n\n\n<h3><a href=\"https:\/\/github.com\/seraph776\/virtaul-enviornment\/blob\/main\/README.md#view-installed-packages\"><\/a>View installed packages<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>pip list<\/code><\/pre><\/div>\n\n\n\n<p>This will display all install packages in your virtual envornment. You should just see&nbsp;<code>pip<\/code>&nbsp;and&nbsp;<code>setuptools<\/code>. It is ok if you get a warning message telling you to upgrade pip. Just ignore it for now.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/user-images.githubusercontent.com\/72005563\/137311780-eed5f19e-de16-46cd-acae-62db07ef5196.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img src=\"https:\/\/user-images.githubusercontent.com\/72005563\/137311780-eed5f19e-de16-46cd-acae-62db07ef5196.png\" alt=\"image showing just pip, and setuptools are installed in new virtual environment.\"\/><\/a><\/figure>\n\n\n\n<h2><a href=\"https:\/\/github.com\/seraph776\/virtaul-enviornment\/blob\/main\/README.md#step-6-install-packages-using-pip\"><\/a>Step 6: Install packages using pip<\/h2>\n\n\n\n<p>The syntax for installing packages is:&nbsp;<code>pip instll &lt;PACKAGE_NAME&gt;<\/code>. FOr this Tutorial we will install&nbsp;<code>matplotlib<\/code>. Type the following command:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>pip install matplotlib<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/user-images.githubusercontent.com\/72005563\/137313068-d47ac29d-e715-40d2-b8f8-b3e81ff55fde.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img src=\"https:\/\/user-images.githubusercontent.com\/72005563\/137313068-d47ac29d-e715-40d2-b8f8-b3e81ff55fde.png\" alt=\"Install matplotlib in virtual environment\"\/><\/a><\/figure>\n\n\n\n<h4><a href=\"https:\/\/github.com\/seraph776\/virtaul-enviornment\/blob\/main\/README.md#clear-your-terminal\"><\/a>Clear your Terminal;<\/h4>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>clear<\/code><\/pre><\/div>\n\n\n\n<p>Now lets see what packages we have in our vortual enviornment with the command:&nbsp;<code>pip list<\/code>. You should now see&nbsp;<code>Matplotlib<\/code>installed as well as other dependancies.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/user-images.githubusercontent.com\/72005563\/137313388-093b723a-8f02-490b-acff-3dc41dd344d2.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img src=\"https:\/\/user-images.githubusercontent.com\/72005563\/137313388-093b723a-8f02-490b-acff-3dc41dd344d2.png\" alt=\"matplotlib installed in virtual environment\"\/><\/a><\/figure>\n\n\n\n<h2><a href=\"https:\/\/github.com\/seraph776\/virtaul-enviornment\/blob\/main\/README.md#step-7-create-a-file\"><\/a>Step 7: Create a File<\/h2>\n\n\n\n<p>Let\u2019s create a file inside our&nbsp;<code>project-1<\/code>&nbsp;name the file&nbsp;<code>main.py<\/code>and type the following code:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import matplotlib.pyplot as plt\nsquares = [1, 4, 9, 16, 25]\nplt.plot(squares)\nplt.show()\n\n<\/code><\/pre><\/div>\n\n\n\n<p>To run the module, type the following comand:&nbsp;<code>python main.py<\/code>&nbsp;and hit enter. You should see the following graph.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/user-images.githubusercontent.com\/72005563\/137314358-c0eaac73-5158-4ebc-b248-9d809e1bc2ae.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img src=\"https:\/\/user-images.githubusercontent.com\/72005563\/137314358-c0eaac73-5158-4ebc-b248-9d809e1bc2ae.png\" alt=\"matplotlib graph\"\/><\/a><\/figure>\n\n\n\n<p>Close the graph when you are done.<\/p>\n\n\n\n<h2><a href=\"https:\/\/github.com\/seraph776\/virtaul-enviornment\/blob\/main\/README.md#step-8-deactivate-virtual-enviornment\"><\/a>Step 8: Deactivate Virtual Enviornment<\/h2>\n\n\n\n<p>Type the following command to deactive the virtual enviornment<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>deactivate<\/code><\/pre>\n\n\n\n<p>You will now notice your command shell has changed again.You should not see the virtual folder name in parentheses anymore.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/user-images.githubusercontent.com\/72005563\/137315285-0f4111ea-ccf6-49b4-88f3-afa3401e90ea.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img src=\"https:\/\/user-images.githubusercontent.com\/72005563\/137315285-0f4111ea-ccf6-49b4-88f3-afa3401e90ea.png\" alt=\"image\"\/><\/a><\/figure>\n\n\n\n<h1><a href=\"https:\/\/github.com\/seraph776\/virtaul-enviornment\/blob\/main\/README.md#conclusion\"><\/a>Conclusion<\/h1>\n\n\n\n<p>This is how you set up a Virtual Environment the easy way using the venv module. This is a very important skill to learn as a Python developer. In the next tutorial I will show you how to implement a Virtual Environment using&nbsp;<a href=\"https:\/\/pypi.org\/project\/pipenv\/\">pipenv<\/a>&nbsp;If you have any questions about this process, please feel free to reach out to me!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Take advantage of virtual environments in Python to create and manage separate environments for your Python projects. <\/p>\n","protected":false},"author":1,"featured_media":71,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[11,24,25,26],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts\/69"}],"collection":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/comments?post=69"}],"version-history":[{"count":7,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts\/69\/revisions"}],"predecessor-version":[{"id":356,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts\/69\/revisions\/356"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/media\/71"}],"wp:attachment":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/media?parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/categories?post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/tags?post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}