Virtual Environments in Python

One of the most important tools Python offers is Virtual Environments, something you will most definitely use for project isolation. In this post I explain in a simple way how it works and how you can set up your own venv for your project.

Daniela Brailovsky
3 min readMar 2, 2022

Last week I cloned a repository of a project I’d worked on in another computer. I hadn’t touched that project for a month, and it was time to make some front end changes, but as soon as I ran the program, I started getting tons of errors. Some of the dependencies were conflicting with the versions I had in that computer, and when I tried changing the versions, I would then have problems with the other Python projects. It was only after a sip of coffee I realised I’d forgot one of the most important things of Python: the Virtual Environment!

Why are virtual environments so useful?

Virtual environments allow us to keep each project isolated from the others. This way, you can have as many projects as you want with different versions of the same library. Think of your projects as boxes. You want to keep each project in a different box, so what you put inside won’t affect the other boxes. This is exactly what venvs do.

Cat in a box
Picture by Jiawei Zhao https://unsplash.com/photos/W-ypTC6R7_k

What a venv really is and how to use it?

A virtual environment is a directory inside your project folder.

Amongst the files inside the venv folder you will find:

  1. The Python version you are using in that venv. You can have copies of different Python versions in each venv.
  2. A folder of all the the third party libraries you have installed in that venv.

To create a virtual environment (with Python 3.3 or above) from your terminal, go to your project folder and write:

python3 -m venv name-of-venv

This will create a venv with the name name-of-venv.

Now in order to use it, you need to activate it:

source name-of-venv/bin/activate

If you are in Windows:

name-of-venv\Scripts\activate.bat

You will know if it’s activated because its name will show between parenthesis in front of the line:

(name-of-venv) Daniela@daniela example-project$

Another way of making sure if you’ve activated it, you can run:

which python

And if the path of the python you are using is inside name-of-venv, then you know it’s activated.

You can run pip list in order to know which libraries you have installed inside the venv:

pip3 list

Once you are finished, you can deactivate it by simply running:

deactivate

Good practices

  1. Don’t commit your venv to source control! You can add venv to the .gitignore so it won’t be committed.
  2. Don’t put your project files inside the venv folder.
  3. Always have a requirements.txt (and definitely commit this to source control). This is so if you need to share your project with others, or if you are cloning it from another computer, you can run it to install all the dependencies with the required versions for your project.

To install the requirements.txt, run:

pip3 install -r requirements.txt

If you need to create the requirements.txt to have all the dependencies you have installed for that project, you can easily run:

pip3 freeze > requirements.txt

And you will have the .txt with all the dependencies and its versions inside.

Conclusion

Virtual environments allow us to keep isolated projects so we can use all the dependencies we want without them affecting other projects.

The basic process of using a venv is to create it → activate it → install the libraries we need / installing the requirements.txt → deactivate

I hope you find this post useful!

Happy coding :)

--

--