Skip to content

Python Virtual Environments Paradise

cover

In this article, I will explain why Python Virtual Environments are so cool when you know how to use them properly. I will also show you how to use them the right way.

What are we talking about ?

Python Virtual Environments are a way to isolate your Python environment from the system's Python environment. This is useful when you are working on multiple projects that require different versions of Python or different versions of the same package.

I have heard many times that people are afraid of using Python Virtual Environments because they think it is complicated and unnecessary. But at the same time, there are more than hundreds of python packages that tries to solve the same problem : pyenv, virtualenv, pipenv, conda, poetry, pew, and many more.

A little bit of history

Let's go back to the basics. Wow there is a built-in module for that : venv ! Who knew ?! It was first introduced in Python 3.3 with the PEP 405 – Python Virtual Environments.

This PEP proposes to add to Python a mechanism for lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (allowing creation of environments with various Python versions) and can have its own independent set of installed Python packages in its site directories, but shares the standard library with the base installed Python.

So what about all these people trying to solve the same problem ? Well, they are trying to make it easier to use and to manage. But in the end, they all provide the same thing : a way to isolate your Python environment.

How to use them the right way ?

You only need to know 3 commands to use Python Virtual Environments the right way.

Create a new virtual environment

python -m venv myenv

This command will create a new directory called myenv that contains a Python environment. You can replace myenv with any name you want. The most common name is venv of .venv. (Don't forget to add it to your .gitignore file if you are using git).

Activate the virtual environment

source myenv/bin/activate

This command will activate the virtual environment. You can see that the prompt has changed. It now contains the name of the virtual environment in the beginning of the line.

(myenv) user@host:~$

You can check the Python binaries that are used by the virtual environment with the following command.

which python

Deactivate the virtual environment

deactivate

This command will deactivate the virtual environment. You are now back to the system's Python environment.

Now what ?

Now that you know how to use Python Virtual Environments, you can use them in your projects.

In my routine, when I start a new project or when I work on an existing project, the first thing I do is to create a new virtual environment. Then I activate it and I install the required packages.

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

This allows me to work on multiple projects that require different versions of Python or different versions of the same package without any conflicts. Without this technique, I would have to deal with a lot of conflicts and I would have to uninstall and reinstall packages all the time.

Conclusion

Python Virtual Environments are a great way to isolate your Python environment from the system's Python environment. They are easy to use and they are very useful when you are working on multiple projects that require different versions of Python or different versions of the same package. You should use them in your projects. It will save you a lot of time and headaches.

And remember, Python already provides a built-in module to create virtual environments. You don't need to use any third-party package to do that. Trust Python, not some random package you found on the internet.