What is a Python Virtual Environment?

In simplest terms, a Python virtual environment is an isolated directory that contains the Python interpreter, packages, dependencies and scripts installed within that isolated directory. A Python virtual environment separates dependencies for different projects by isolating the dependencies in different locations.

Why and When Do We Need Virtual Environments?

Different Python applications require different dependencies, but they also can rely on some of the same dependencies. In some cases, two or more Python applications require different version of the same Python package or an application you built with one Python package may no longer work if you updated that package to the latest version.

For example, consider a scenario where you have the default Python environment shared by all your applications. You developed an application A which requires version 1.2 of a Python package X. At a later stage, you need to develop an application B which also requires the package X. However the application B requires version 1.5 of package X. If you upgrade to version 1.5, your application A might break since application A relied on version 1.2 of package X, which has now been updated to version 1.5.

Python virtual environments help you solve this problem by isolating dependencies for different projects. Let’s explore how you can create virtual environments in Python.

Creating a Python Virtual Environment

To create a virtual environment, you can use the virtualenv package.

The following command installs the virtualenv package.

pip install virtualenv

As an example, in this tutorial, we’ll create two virtual environments for two sample projects MyProject and MyProject2. The directory structure for these projects is shown below.

Output:

Python sample projects


Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

Yes, I'll take a free Python Developer Kit

Ideally, you should create a virtual environment in the root directory of the project that will use this environment. For that reason, we’ll create our virtual environments in the root directories for our projects MyProject and MyProject2.

Let’s first create a virtual environment for the MyProject application.

To create a virtual environment you have to specify the command virtualenv followed by the name of your virtual environment. The script below creates a virtual environment named proj_env in the root directory of the MyProject.

(base) manimalik@manimalik-Precision-3561:~$ cd Projects/MyProject

(base) manimalik@manimalik-Precision-3561:~/Projects/MyProject$ virtualenv proj_env

Once you execute the above command, you’ll see the following output. It shows that a virtual environment has been created. The default Python version and other tools and packages installed are shown in the output.

Python virtualenv command

Now if you look at your directory structure in the MyProject folder, you’ll see a new folder proj_env which contains packages for your virtual environment.

Python project virtualenv

To activate your virtual environment, in Linux (ubuntu) run the command, where environment_name is the name you gave your virtual environment:

source environment_name/bin/activate

In windows, you need to run the following command:

environment_name\Scripts\activate

We’ll be using the Ubuntu OS, in this tutorial, so we’ll run the following command to activate our proj_env.

source proj_env/bin/activate

Once you activate a virtual environment, you can see its name prefixed with your commands as shown below.

activate virtualenv

Now, any package you install from within your virtual environment, will be installed in the local environment proj_env. Let’s install the NumPy library with version 1.20.0.

pip install numpy==1.20.0

Output: package install

From this virtual environment, when you run any Python project within the proj_env, it will use the NumPy version 1.20.0.

To deactivate a virtual environment, you can use the deactivate command as shown below:

(proj_env) (base) manimalik@manimalik-Precision-3561:~/Projects/MyProject$ deactivate

Suppose you want to use a different NumPy version for your other project, MyProject2. You can create a new virtual environment for that project, activate that environment, install the required NumPy version and run MyProject2 in that environment.

We’ll create a virtual environment proj_env2 in the root directory of our MyProject2 application. The steps are the same as you did for creating proj_env virtual environment, so we’ll skip them and directly move to the step where we will install the NumPy version 1.22.0 in the proj_env2 virtual environment.

(proj_env2) (base) manimalik@manimalik-Precision-3561:~/Projects/MyProject2$ pip install numpy==1.22.0

At this point, the NumPy version in the proj_env virtual environment is 1.20.0 as shown below.

numpy installation

The NumPy version in the virtual environment proj_env2 is 1.22.0, as shown below.

numpy installation 2

By using virtual environments, we were able to separate dependencies for our two projects. Now, the project using the NumPy version 1.20.0 can be run with the virtual environment proj_env, while the project using the NumPy version 1.22.0 can be executed in the virtual environment proj_env2.


Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

Yes, I'll take a free Python Developer Kit