Managing Python Virtual Environments using Virtualenv

Joseph Kariuki
6 min readJul 21, 2023

In my article introducing virtual environments in Python, we had a look at what is a virtual environment in Python and also highlighted reasons why we need to manage packages as well as listing the tools that are involved. In this article, we shall see how to manage virtual environments in Python using virtualenv.

Python’s logo is symbolized by snake
Photo by David Clode on Unsplash

This article is recommended for beginners in Python programming or advanced users who would want to refresh their knowledge of Python virtual environments. You must have Python on your PC to make good use of this article. If you do not have one and wondering how to install it, please make reference to these tutorials:

  1. Installing Python on Windows
  2. Installing Python in Linux
  3. Installing Python in MacOS

Let’s begin.

Why Do You Need Virtualenv?

Chances are that you already have the virtualenv installed in your PC especially if you are using a Python version that is higher than 3.3. This is because, since version 3.3, virtualenv was integrated into Python’s standard library under the venv module. But why do you need virtualenv and why are we discussing it here? Let me try to answer that in the bullets below:

  • As a developer, more often than not, conflicts brought on by installing the incorrect version of a package or its dependency can be a headache and deterrent towards achieving goals in software development.
  • As a developer, you frequently have to install various versions of the same library for various projects. How do you do that? Virtual environments are the answer.
  • You will need to install packages that aren’t included in the Python standard Library e.g. Django, Flask, pylibmc, etc. if you’re working as a Python developer on some project. How do you do that? Virtual environments.
  • If you are a regular user (with no administrative access), you can construct and use virtual environments because they don’t require superuser (root) rights. As long as you have Python installed on your PC.

Virtualenv vs Venv

The tool for building isolated Python environments is what is referred to as virtualenv. As mentioned above, it has been included in the standard library as the venv module starting with Python 3.3. Not all of this library’s functionalities are available in the venv module. The Venv module:

  • Lacks app-data seed cache therefore slower than virtualenv. This is a platform-specific application data folder cache that is used by virtualenv for faster installation via caching.
  • Is not upgradeable via the Python Package Index, commonly known as pip unlike its counterpart, virtualenv.
  • Cannot define/describe virtual environments without creating them like virtualenv via its programmable Application Programming Interface (API)

Because of its features, virtualenv has various abstractions of it created which include virtualenvwrapper, nox, tox, and pew. Some of the abstractions will be covered in future articles.

How To Use Virtualenv

We have already highlighted that virtual environments address the challenges that arise from development requirements needing to use different versions of a given module across different projects. In this article, we are using the Linux terminal. Let’s see how virtualenv works.

Installing Virtualenv

To install virtualenv as a user package, assuming that you already have Python installed on your PC, run the following command.

python3 -m pip install — user virtualenv

You can confirm this installation using the command and the output should appear as shown below the command. The output shows the virtualenv’s installation location

which virtualenv
/home/testuser/.local/bin/virtualenv

Now we have installed virtualenv successfully. Let’s create a virtual environment with it next.

Creating a Virtual Environment using Virtualenv

Navigate to your working directory and run the following command

virtualenv proj_env

The terminal output on my PC appears as shown below confirming the successful creation of the virtual environment.

created virtual environment CPython3.10.6.final.0–64 in 622ms
creator CPython3Posix(dest=/home/testuser/project_folder/proj_env, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/testuser/.local/share/virtualenv)
added seed packages: pip==23.1.2, setuptools==68.0.0, wheel==0.40.0
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

Let’s activate the virtual environment using a similar approach used in my previous article on Python virtual environments.

source proj_env/bin/activate

Alternatively, you can activate using the dot command preceding the path to the activate script as shown below.

. proj_env/bin/activate

You will notice the name of the virtual environment inside parentheses at the start of the terminal output as shown below.

(proj_env) $

Another item that we can check is the location of the pip files, which should be in a different location from that of the operating system.

(proj_env) $pip -V
pip 23.1.2 from /home/testuser/project_folder/proj_env/lib/python3.10/site-packages/pip (python 3.10)

Installing Packages With Virtualenv

Now that we have the virtual environment active, let’s check the default packages that are already installed.

(proj_env) $pip list
Package Version
— — — — — — — — -
pip 23.1.2
setuptools 68.0.0
wheel 0.40.0

Let’s highlight what these packages are and their importance in the Python virtual environment.

  • Pip: Python’s package installer that is used to install other packages from the Python Package Index (also known as the cheese shop) or an alternative index.
  • Setuptools: An essential tool in Python development that helps in packaging projects written in Python to libraries and/or programs.
  • Wheel: A tool that is an extension of setuptools and aims at improving Python packaging in size and helps avoid building packages from source therefore quite important in the virtual environment.

The numbers that appear after the package names are versions that are installed at the time of writing this article. In your case, the versions may differ from the ones shown above.

Let’s install the Flask package, as an example, using the pip install command.

(proj_env) $pip install flask
Collecting flask
Downloading Flask-2.3.2-py3-none-any.whl (96 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 96.9/96.9 kB 344.3 kB/s eta 0:00:00
Collecting Werkzeug>=2.3.3 (from flask)
Downloading Werkzeug-2.3.6-py3-none-any.whl (242 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 242.5/242.5 kB 378.7 kB/s eta 0:00:00
Collecting Jinja2>=3.1.2 (from flask)
Downloading Jinja2–3.1.2-py3-none-any.whl (133 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.1/133.1 kB 619.4 kB/s eta 0:00:00
Collecting itsdangerous>=2.1.2 (from flask)
Downloading itsdangerous-2.1.2-py3-none-any.whl (15 kB)
Collecting click>=8.1.3 (from flask)
Downloading click-8.1.6-py3-none-any.whl (97 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.9/97.9 kB 498.9 kB/s eta 0:00:00
Collecting blinker>=1.6.2 (from flask)
Downloading blinker-1.6.2-py3-none-any.whl (13 kB)
Collecting MarkupSafe>=2.0 (from Jinja2>=3.1.2->flask)
Downloading MarkupSafe-2.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Installing collected packages: MarkupSafe, itsdangerous, click, blinker, Werkzeug, Jinja2, flask
Successfully installed Jinja2–3.1.2 MarkupSafe-2.1.3 Werkzeug-2.3.6 blinker-1.6.2 click-8.1.6 flask-2.3.2 itsdangerous-2.1.2

You will notice that this installs additional packages well known as dependencies.

Reproducing Virtual Environment

Replicating a virtual environment is common. Assume that the project you have been working on for weeks will be completed by your coworker. On her workstation, she must install the exact same software in the proper versions in a virtual environment. You must first use the pip freeze command to identify all the dependencies that have been installed in the project's virtual environment in order to build identical environments.

Let’s list the installed package details, pip show command

(proj_env) $pip show flask
Name: Flask
Version: 2.3.2
Summary: A simple framework for building complex web applications.
Home-page:
Author:
Author-email: Armin Ronacher <armin.ronacher@active-4.com>
License: BSD-3-Clause
Location: /home/testuser/project_folder/proj_env/lib/python3.10/site-packages
Requires: blinker, click, itsdangerous, Jinja2, Werkzeug
Required-by:

Also, let’s check the installed packages again as before. This shall list all installed packages and their versions.

(proj_env) $pip list
Package Version
— — — — — — — — — -
blinker 1.6.2
click 8.1.6
Flask 2.3.2
itsdangerous 2.1.2
Jinja2 3.1.2
MarkupSafe 2.1.3
pip 23.1.2
setuptools 68.0.0
Werkzeug 2.3.6
wheel 0.40.0

To reproduce the virtual environment packages and their versions, we’ll create the proverbial requirements.txt file using pip freeze

(proj_env) $pip freeze > requirements.txt

And let’s print out the contents of the file. You can share this file with a colleague or fellow developers and they can install the same version of packages as yours.

(proj_env) $cat requirements.txt 
blinker==1.6.2
click==8.1.6
Flask==2.3.2
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
Werkzeug==2.3.6

Let’s now see how we can begin wrapping it up next.

Deactivating Virtualenv

Once you are done with the installation of your required packages and sharing them with colleagues or developers, it may be time to go back to your usual terminal and exit or do other tasks without getting in the way of this virtual environment.

To deactivate the virtual environment, run the deactivate command and you will be back to the initial terminal without the name of the virtual environment in parentheses.

(proj_env) $deactivate
$

We have concluded the basic steps of managing the virtual environment in Python, in this article we have made use of virtualenv, which is one among several virtual environment tools, some of which are abstractions.

Conclusion

I trust that this article regarding managing virtual environments in Python will be useful to you going forward. You can refer to my article that precedes this which generally highlights in more detail why you need to use virtual environments and how to set them up without a third-party tool or software.

In future articles, we shall have a look at using a third-party tool to manage virtual environments in Python. Share this article and do not hesitate to comment with questions or recommendations. Happy learning!!!

--

--