Virtual environments in Python

lordebasta
Analytics Vidhya
Published in
4 min readDec 10, 2021

--

Python projects require different packages to work or the same packages but with different versions of them. If you have two projects on your machine that require the NumPy package, but with two different versions is a problem, because you can’t have more than one version installed of course. To resolve this problem and to make things cleaner, there are virtual environments.

If you think it could be useful for you, check out this other story:

Imagine that each project has its own python interpreter and its own packages. They basically run on different environments, so two projects in two different virtual environments don’t share anything. So how to do that?

My current setup is macOS Monterey with python 3.9.9.

Now I have this empty folder called virtual_environments. Here I want to create a couple of projects for different things. So first I am gonna create a new folder for the first project, let’s call it project_one. In project_one if I call python3 obviously refers to the python 3 interpreter installed on my machine.

To create a new virtual environment, run the command:

python3 -m venv venv

You are asking the module venv of python3 to create a new virtual env, stored in the folder venv. As you can see a folder venv has been created and inside it, in bin there is also the python interpreter of this environment.

In the bin folder there are also a bunch of scripts called activate, depending on which shell you are using. In my case to activate the virtual env (we created it, but we haven’t told the process to use it yet!) I just have to run the script activate with the command source.

source venv/bin/activate

As you can see now there is a little (venv) before the user, notifying the fact that we are working inside the virtual env. Let’s try to test it. If I run

pip3 install pandas numpy

the packages will be installed inside the virtual env and indeed they are:

Now I can check that the packages won’t be outside of the virtual environment. To exit the virtual env I have to run the command deactivate:

It works! Now for the second project, I just do the same and I’ll add the packages there that I need.

Last thing to mention. Let’s say that I want to share my project with Bob. I can share with him the folder venv too, but since it’s quite big (mine without any package installed is 13 MB) isn’t so efficient. He can do it for himself. The problem is that he must know what packages he has to install. To resolve this I can create a file called requirements.txt (standard name) and write there the list of packages required:

pip3 freeze > requirements.txt

As you can see now in the requirements.txt there is the list of the packages. Now Bob can create his virtual env and then run the command

pip3 install -r requirements.txt

This command will automatically go look in the file and install the required packages. Quite useful and nice to have.

--

--