Exploring Virtual Environments in Python

Ashwin Nair
5 min readSep 11, 2020

This is the first article about virtual environments in Python.

Do you want to create a virtual environment? Or perhaps you already created one, and found yourself stalled after making it? Don’t worry! This article will address why we need virtual environments and how to create them.

Virtual environments[1] allow you to have a completely isolated environment for your project. This means that the dependencies for your projects will not interfere with the dependencies of other projects. Python standard library has a module called venv[2], which was first introduced in Python 3.3, just for this purpose. Let's try to make a virtual environment with venv. But first, I suggest going to your file directory and making a few new folders. See below:

This folder structure will allow us to test the virtual environment system simply. test.py contains a simple print statement that says "test.py". test2.py says "test2.py". Pretty simple, right?

Now let’s move on to creating the virtual environment. Go to your terminal application, and type the following line:

Make sure you’ve correctly typed everything; typos matter. For example, if you write /Project3/ instead of /Project1/, Python will create a new directory called Project3, and create a virtual environment within it. Now, let's check the Project1 folder, and you should see 3 new folders, a new config file, along with our previously made test.py file.

If your directory looks like mine, congratulations! You’ve made your first virtual environment!

The Project folders were created to show two separate projects, both of which will have two separate virtual environments. The pyvenv.cfg file is a file that stores some metadata such as the version of the Python interpreter. Now to start working within the environment, you have to activate it. This is done from the terminal. Open your terminal application once more and type the following lines:

Now, you’re in the project directory. Next, let’s activate the virtual environment.

If this worked, you should notice an immediate change to your terminal. Your terminal should’ve changed from:

to

Adding the name of the virtual environment’s name at the beginning of the prompt is Python’s way of telling you that you’re in an active virtual environment. Please note that the percent sign at the end of my prompt signifies where the user input starts. Your terminal may use another symbol, but they all have the same basic meaning.

Let’s try installing a package using pip. Pip[3] (preferred installer program) is the preferred program installer. From Python 3.4 onwards, venv automatically installs pip into the virtual environment. Keep in mind that if you're using Python 3 like me, you should do pip3 install[4] instead of pip install, to specify which Python installation you want to use. To uninstall a package you've installed, you can use pip3 uninstall [package name][5] to uninstall it. For this example, I chose the Pandas[6] library, a very popular data analysis module. First, let's deactivate our virtual environment, simply by typing deactivate:

Now, the (Project1) text will disappear from the prompt, and this signifies that we are no longer in the virtual environment. Now let's try installing pandas:

Since we’re not currently in a virtual environment, installing pandas will install it to the system library, and not into our virtual environment. If you have pandas installed, you’ll see a different screen, which shows pandas being downloaded. In my case, you can see that I already have pandas installed. If so, try pip3 install pandas again, and you should see the screen above. Now we've established that pandas are downloaded. Let's activate the virtual environment and try installing pandas again:

What does this mean? This signifies that our virtual environment is isolated and working; no changes in the virtual environment will affect the rest of the system and vice versa. This is very important since now our virtual environment will work independently of what’s happening outside of it; even if we were to update to a new version of Python on a global scale, we would be using the same version within the Virtual Environment.

Now say you want to transfer your installed packages into another virtual environment. This can be simply done by using a requirement file[7], created by using the pip freeze[8] command. To demonstrate this, we'll make use of our Project2 directory, and make a virtual environment inside it. First, let's create the requirements file.

All you have to do is type:

This will record all the installed packages and their respective dependencies from Project1 into the newly created requirements.txt file. Now, let's go to the Project2 directory, and create a new virtual environment:

Now, copy and paste the requirements.txt file into the Project2 directory. Next, activate the virtual environment within the Project2 directory, and install from the requirements file:

To see all installed packages, try pip3 list[9]. The output will look something like this:

To get more information on one of these installed packages, you can try pip3 show[10]. It's very helpful in understanding what each package is used for since packages can quickly build up when working on a project.

To get a specific version of a particular package, you can use pip3 install numpy==1.19.1; This will install version 1.19.1 of the numpy module:

You can also search for modules right from your terminal, using pip search [topic][11]. See below:

In closing, this article showed you the basics of a virtual environment and some of the pip commands that are often used to customize them. We learned how to install, search for, export, and find documentation on Python packages, all with pip. I hope that this article helped you set up your virtual environment; keep your eyes open for the next article!

References

--

--

Ashwin Nair

Pursuing my bachelors in Computer Science, while learning Natural Language Processing with SpaCy and Python.