How to create Virtual Environments in Python

Virtual environments can be described as isolated installation directories. They allow you to localize the installation of your project’s dependencies, without forcing you to install them system-wide. You can have multiple environments, with multiple sets of packages, without conflicts among them. This way, different projects’ requirements can be satisfied at the same time.

Tools used for creating Python virtual environments

Libraries

1. venv

venv is available by default in Python 3.3 and later.

To create a virtual environment use:

python3 -m venv venv

Activate an Environment:

source venv/bin/activate

This ensures that only packages under `my-env/` are used.
You will notice that the name of the environment is shown on the left of the prompt. This way you can see which is the active environment.

You can install packages one by one, or by setting a `requirements.txt` file for your project.

pip install some-package
pip install -r requirements.txt

If you want to create a `requirements.txt` file from the already installed packages, run the following command:

pip freeze > requirements.txt

The file will contain the list of all the packages installed in the current environment, and their respective versions. This will help you release your project with its own dependent modules.

If you are done working with the virtual environment you can deactivate it with:

deactivate

This puts you back to the system’s default Python interpreter with all its installed libraries.

To delete an Environment simply delete the environment folder.

2. Virtualenv

Virtualenv is a tool used to create isolated Python environments. It creates a folder which contains all the necessary executables to use the packages that a Python project would need.

To create a virtual environment use:

virtualenv my-env

This creates a folder in the current directory with the name of the environment (`my-env/`). This folder contains the directories for installing modules and Python executables.

You can also specify the Python version you want to work with:

virtualenv --python=/usr/bin/python3.7 my-env

You can list the available environments with:

ls virtualenv

3. pyenv

pyenv simple Python version management based on rbenv. Used together with pyenv-virtualenv plugin.

To create a virtual environment use:

pyenv virtualenv venv37

4. pyvenv

Deprecated in Python 3.6.

Dependency managers

1. Pipenv

Pipenv manages dependencies on a per-project basis. It is like Node.js’ npm or Ruby’s Bundler.

To create a virtual environment use:

pipenv --three

Separate software stacks

1. Conda

Conda is a package, dependency and environment management for many languages, including Python.

To create a virtual environment use:

conda create --name my-env

Conda will create the corresponding folder inside the Conda installation directory.

More Information:

Get Free Email Updates!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailChimp ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.

Leave a Reply