DEV Community

Cover image for Managing python virtual environments
Import Sys
Import Sys

Posted on

Managing python virtual environments

If you are working on multiple projects you might have run into a problem — different projects requiring different versions of the same package.

Today we are going to solve this with python virtual environments. You'll learn what they are, how to create and use them, and how they actually work under the hood.

Why to use virtual env

Imagine you are working on the two projects that both use the requests package as a project dependency.
Now you will have a dilemma.
If you installed requests package system-wide you will have to use same package version for the both projects.
But, let's say your first project is in a production state and you have to stick with the specific package version. On the other hand while building your second project you want to use new features from the latest package release.

Luckily you can use virtual environments!

What is the virtual env

Virtual environment is the isolated python environment which allows you to manage dependencies separately.

Create virtual env

Starting from the version 3.3 python provides built in mechanism to create virtual environments - venv module.
Let's create virtual environment.

python -m venv ./virtualenv
Enter fullscreen mode Exit fullscreen mode

To create virtual environment you have to run venv module specifying the path where virtual environment will be created.

Activating virtual env

To be able to use specific virtual environment you have to activate it first.

source virtualenv/bin/activate
Enter fullscreen mode Exit fullscreen mode

If you are using Windows you should use activation script for windows also available in the virtual env folder.

virtualenv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

Now you can see the terminal prompt has changed which means that you are working in the virtual environment.

Active virtual environment

Installing packages

Package installation is exactly the same as without using virtual env. But now the packages are isolated to the current virtual environment.
Also now it much safer to use pip directly since it bound to the specific python environment

pip install requests
Enter fullscreen mode Exit fullscreen mode

Now it have much more sense to provide requirements.txt file with the project since you can isolate packages for every project. And the installation process does not differs at all.

Let's take the requirements file from the PIP tutorial and install all the packages listed.

cat requirements.txt
Enter fullscreen mode Exit fullscreen mode

requirements.txt

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Now we can check the installed packages in the virtual environment

pip list
Enter fullscreen mode Exit fullscreen mode

installed packages

Cool, we have our packages installed. But how we make sure that they are isolated?

Deactivating virtual environment

Let's leave virtual environment by running deactivate command

deactivate
Enter fullscreen mode Exit fullscreen mode

Note that all the packages you've installed in the virtual environment are still there you just leave environment itself.

And now if we check the packages list

python -m pip list

python -m pip show numpy
python -m pip show openpyxl 
Enter fullscreen mode Exit fullscreen mode

packages not found

You see that there are no packages that were installed inside the virtual environment.
So you can tell isolation is working.

How it works

Okay, but what is the magic actually?
Let's activate our virtual environment once again and see how it works.

source virtualenv/bin/activate
Enter fullscreen mode Exit fullscreen mode

When we run python or pip, what do we actually run? Let's check

which python 
which pip
Enter fullscreen mode Exit fullscreen mode

python location

As you can see python interpreter as well as pip are located inside our virtual environment.

Every time you create virtual environment you get dedicated python interpreter and package manager.

Ok, but how does system knows that it should run this exact python interpreter?

Let's check the PATH variable

echo $PATH
Enter fullscreen mode Exit fullscreen mode

path env variable

As you can tell our virtual environment now is the first location where system will look for the executable. So activating virtual environment will automatically update your PATH variable adding virtual env location on top.

Ok, cool. But what about packages? Let's see

ls -l virtualenv/lib/python3.14/site-packages
Enter fullscreen mode Exit fullscreen mode

packages folder

Here they are! All installed packages are stored inside virtual environment too.

So that's how you can have different dependencies of different versions for your every project. Just create own virtual environment for every project!

Removing venv

Obviously you can remove virtual environment. You can simply remove the virtual environment folder, and that's it. But do not forget to deactivate it first

deactivate
rm -rf virtualenv
Enter fullscreen mode Exit fullscreen mode

And that's virtual environments! Now you know how to keep your project dependencies clean and isolated.

Top comments (0)