2022-05-12    Share on: Twitter | Facebook | HackerNews | Reddit

Creating Virtual Environments in Python

Python virtual environments are an essential tool for isolating project dependencies and avoiding conflicts between different projects. This article will guide you through methods of creating virtual environments in Python.

Most popular methods

Using venv

venv is a module that comes pre-installed with Python 3.3 and later versions. It allows you to create lightweight virtual environments with their own site directories.

To create a virtual environment using venv, use the following commands:

# Linux and macOS
python3 -m venv virtual_environment_name

# Windows
py -m venv virtual_environment_name

If you want to create a virtual environment with a specific Python version, e.g., 3.9, you can do so by specifying the Python version as follows:

python3.9 -m venv virtual_environment_name

To activate this environment and install any packages, use the following commands:

# Linux and macOS
source virtual_environment_name/bin/activate

# Windows
.\virtual_environment_name\Scripts\activate

Using virtualenv

virtualenv is a third-party Python package that you can install using pip. It allows you to create multiple side-by-side environments.

First, install virtualenv using the following command:

brew install virtualenv

Then, create a virtual environment with a specific Python version, e.g., 3.9, as follows:

virtualenv -p 3.9 $HOME/.virtualenvs/safeeyes

Using pyenv

pyenv is a powerful tool for managing multiple Python versions. It doesn't come pre-installed with Python, so you'll need to install it separately. Once installed, you can use it to create virtual environments.

Detailed instructions on how to use pyenv for creating virtual environments will be covered in a separate article.

Using conda

conda is a package, dependency, and environment management tool for any language, but it is particularly popular in the Python community. It comes pre-installed with the Anaconda Python distribution.

To create a virtual environment with conda, use the following command:

conda create --name virtual_environment_name python=3.9

To activate the environment, use:

conda activate virtual_environment_name

Alternatives

While venv, virtualenv, and pyenv are the most commonly used tools for creating virtual environments in Python, there are alternative methods available. Here are a few:

Using pipenv

pipenv is a tool that aims to bring the best of all packaging worlds to the Python world. It harnesses Pipfile, pip, and virtualenv into one single command.

Install pipenv using pip:

pip install pipenv

Then, to create a new virtual environment for your project, navigate to your project directory and run:

pipenv install

This command will create a new virtual environment (if one doesn't already exist) and install the packages specified in the Pipfile.

To activate the environment, use:

pipenv shell

Using Poetry

Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

Install Poetry:

curl -sSL https://install.python-poetry.org | python -

To create a new virtual environment and install dependencies, navigate to your project directory and run:

poetry install

This command will create a new virtual environment (if one doesn't already exist) and install the packages specified in the pyproject.toml file.

To activate the environment, use:

poetry shell

Using Docker

While not a Python-specific tool, Docker can be used to create isolated environments for Python applications. A Docker container can be thought of as a lightweight, standalone, and executable software package that includes everything needed to run a piece of software, including the system tools, libraries, and settings.

To use Docker, you need to create a Dockerfile that specifies the Python version and the dependencies your application needs. Then, you can build a Docker image from this Dockerfile and run your application inside a Docker container based on this image.

Using PDM

PDM is a modern Python package manager with PEP 582 support. It uses the pyproject.toml file to manage dependencies and environments, which makes it compatible with other tools that use this standard.

To create a new project with a dedicated environment, use:

pdm new my_project

To activate the environment, use:

pdm shell

Using Hatch

Hatch is a productivity tool designed to make your workflow easier and more efficient, while also reducing the number of other tools you need to know. It is heavily inspired by npm.

To create a new project with a dedicated environment, use:

hatch new my_project

The command above will create a new directory named my_project, set up a new virtual environment, and initialize a new Git repository.

Using Pipx

Pipx is a tool to help you install and run end-user applications written in Python. It's like pip, but for whole Python applications rather than for Python libraries.

To install a Python application in its own isolated environment, use:

pipx install my_project