You are currently viewing Python Create venv Virtual Environment

How to create a virtual environment using venv module in Python? The python venv module is used to create a virtual environment from Python 3.3 or higher. This venv module is included in the Python standard library hence it is not required to install in order to use it.

Advertisements

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

1. Create a Virtual Environment on Linux/Mac

First, let’s create a virtual environment in Python by running venv command. This environment is created on top of the existing python installation hence it also uses the same python version.

The below example creates a virtual environment dev-env under the current directory. Note that the virtual environment is just a directory that segregates the module installations for each application.


# Create virtual environment
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 create virtual environment

If you wanted to create a virtual 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.

2. Create a Virtual Environment on Windows

If you are using windows, use the following commands. You need to change the python version according to your installation version.


# On windows

# Create virtual environment
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

Once it is created, you need to activate the virtual environment before using it.

3. Files Created with Virtual Environment

Let’s understand the files that are created with the virtual environment in Python. 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 virtual environment

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.

4. Create Multiple Virtual Environments

To create multiple virtual environments at a time use the python -m venv with space-separated environment names. This creates two directories dev-env and qa-env.


# Create multipl environments
python3 -m venv dev-env qa-env 

5. venv Module Options

Following are the different options you can use while creating a virtual environment in Python by using the venv command.

OptionDescription
–symlinksUses symbolic links instead of copying files from the current python installation.
–copiesUses copy instead of symlinks from the current python installation.
–clearDelete the existing environment if exists before creating a new one.
–upgradeUpgrade the version to use this version of Python.
–without-pipSkips installing pip on a new environment.
–upgrade-depsUpdates core dependencies.
–prompt PROMPTProvides an alternate prompt prefix for this environment.

Conclusion

In this article, you have learned how to create a virtual environment in Python by using the venv module. Also learned how to create multiple environments at a time.