You are currently viewing Python Virtual Environment – venv Module

The venv module is used to work with the virtual environment in Python. This module is available since Python 3.3 or higher. This venv module is included in the Python standard library hence it is not required to install and can be used directly.

Advertisements

In this article, I will explain what is a virtual environment, why we need it, and how to create, activate, deactivate, and remove it with examples.

1. What is a Virtual Environment

A virtual environment is a way to manage multiple parallel instances of Python binaries, including copies of its support utilities. A virtual environment is created on top of an existing Python installation, known as the virtual environment’s “base” Python.

Any package installed in a virtual environment can be seen only in that virtual environment without changing other environments.

2. Why do we need Virtual Environment

When you are working on multiple applications in Python you may be required to run application X with a set of third-party libraries of a specific version and another application Y with another or the same set of libraries with different versions. And you wanted to run these applications with their specific dependency libraries without impacting each other. This is not possible with one installation of Python.

To overcome this problem you can create a virtual environment for each application and install third-party libraries for each environment without conflicting with other versions.

From our example, application A will have its own virtual environment with libraries required to run and application B will have its own virtual environment with its own libraries.

3. Virtual Environment venv Module

The venv module comes with the Python standard library since Python 3.3 hence it can be used directly without any additional installation.

When you create an environment, it uses the current Python version. Let’s create a virtual environment by running venv command. The below example creates a virtual environment dev-env under the current directory.

A virtual environment created from a venv module is a self-contained directory that contains a Python installation for a particular version of Python, plus a number of additional packages.


# Create virtual environment in Linux/MacOS
python3 -m venv dev-env

This creates a dev-env directory within the current path, you can run ls command to get the sub-directories of the virtual environment. You should see something like the below if you are using MacOS, Linux, or Unix.

python virtual environment

If you wanted to create an environment at a custom location, just specify the absolute path along with the environment name.


# Create virtual environment
python3 -m venv /path/to/virtual/environment/dev-env

This creates all parent directories that don’t exist already. If you are using windows, use the following commands.


# Create virtual environment in windows
c:\>c:\Python39\python -m venv c:\path\to\virtual\environment\dev-env

# OR, if you have added python to PATH & PATHEXT
c:\>python -m venv c:\path\to\virtual\environment\dev-env

4. Default Files with Virtual Environment

Let’s understand the files that are created with the environment. Mainly your newly created environment directory contains a key file pyvenv.cfg that contains the home variable with the current python location and python version as shown below.

python venv module

It also creates a bin  subdirectory containing a copy/symlink of the Python binaries. It also creates empty lib/pythonX.Y/site-packages and include subdirectories.

If you are using windows, it creates scripts instead of bin and it creates empty Lib\site-packages.

5. Upgrade Environment with New Python Version

Use --upgrade to upgrade the environment directory to use the current version of Python, assuming Python has been upgraded in place.


# Upgrade python version
python3 -m venv --upgrade dev-env

6. Activate the Environment

To use the environment you need to activate it first by sourcing an activate script in its bin directory. Depending on OS and shell version you use the commands are different to activate the virtual environment.


# Activate environment
source dev-env/bin/activate

On Windows using the command prompt use the following


# Activate environment on windwos
dev-env\Scripts\activate.bat

7. Deactivate Environment

For any reason, if you wanted to deactivate the virtual environment use the deactivate by typing deactivate in your shell.


# Deactivate environment

# On Linux/MacOS, windows powershell
deactivate

# On windows
Scripts/deactivate.bat

8. Removing the Virtual Environment

Python virtual environments are self-contained meaning all the packages, libraries, and configuration exists in a single directory hence, when you no longer needed a virtual environment just delete the directory which removes the environment and all its files. Here is an example.


# Delete virtual environment
rm -R /path/to/virtual/environment/dev-env

Note that before removing a directory, you should come out of it as you can’t delete the directory you are already in.

9. Help Command

To understand other commands to use with the Python venv module use the venv -h (help) option.

python venv module

Conclusion

In this article, you have learned the venv module is used to work with the virtual environment in Python. This module comes with Python standard library hence, it can be used directly without any installation. Python environments are used to segregate the environment for each application and sometimes for each environment (dev, qa, uat e.t.c). So each environment will have its own set of installations and configurations e.t.c