Published on

Creating a Python Virtual Environment

Authors
  • avatar
    Name
    Tinker Assist
    Twitter

tailwind-nextjs-banner

Why Create a Python Virtual Environment?

Python uses the Python Installer Package (pip) to keep track of libraries of code you have installed. These libraries allow you to use code other people have written to help in whatever project you might be working.

When you typically install python packages using "pip install", you are installing packages to your global Python install. Thus, when you run a python program on your device, these global packages will be utilized. The problem is, when you have different python projects you are working on on your device, it's extremely unlikely that all of those projects use the same dependencies. Thus, using your global packages can produce unpredictable results and even result in errors and critical faults.

Python virtual environments allow you to quickly and easily install a fresh version of python for each project so you can install only the dependencies you need.

Creating a Virtual Environment

Let's start by creating a fresh folder on our desktop and opening it in Visual Studio Code (this is my preferred text editor). I'll open a new terminal at the bottom as well. Here is what my setup looks like:

Fresh VSCode Window with terminal and file explorer

Now, assuming you already have python installed on your device, go ahead and run "pip list" in your terminal.

For me, this lists dozens and dozens of packages I have installed on my device.

C:\projects\virtualenv-test> pip list

Package                      Version
---------------------------- -----------
anyascii                     0.3.1
asgiref                      3.5.2
asttokens                    2.2.1
async-timeout                4.0.2
backcall                     0.2.0
beautifulsoup4               4.11.1
birdsong                     1.3.0
bleach                       6.0.0
build                        0.10.0
cachetools                   5.3.0
certifi                      2022.9.24
charset-normalizer           2.1.1
click                        8.1.3
colorama                     0.4.6
comm                         0.1.2
contourpy                    1.0.7
cycler                       0.11.0
dash                         2.7.0
dash-core-components         2.0.0
dash-html-components         2.0.0
dash-table                   5.0.0
debugpy                      1.6.6
decorator                    5.1.1
...

If I pull some script off the internet that leverages some of these libraries, what are the chances it works with the specific version of all these dependencies? It's very unlikely. This is why a virtual environment is necessary.

Venv

To create a virtual environment with the name .venv, execute the following command in the terminal.

C:\projects\virtualenv-test> python -m venv .venv When you do this, you should see a new folder .venv created in your current folder. For me, this shows up at "C:\projects\virtualenv-test.venv".

Activate your Virtual Environment

Now that we have created a new environment, we need to activate it so we are using it and not our global python install. To do this, go back to your terminal and run the following command:

C:\projects\virtualenv-test> .venv\Scripts\activate

This runs the activate script in our virtual environment folder, which means our terminal is now pointing at a new version of python that is located at .venv\Scripts\python.exe

You should see an indicator on the far left of your command line that indicates you have successfully activated your virtual environment. For me, it looks like this:

(.venv) C:\projects\virtualenv-test>

So what does this mean again?

Since we are pointing at a fresh install of python in our environment, we have no packages installed! This is good, because it means we can now install only the packages we need for whatever project we are working on.

To confirm we have a fresh python install, go ahead and run "pip list" like we did earlier. Now, your environment and mine should look exactly the same, with only the default packages listed:

(.venv) C:\projects\virtualenv-test>pip list

Package    Version
---------- -------
pip        22.2.2
setuptools 63.2.0