Skip to content

Python Virtual Environments

Published On:
Jun 28, 2019
Last Updated:
Oct 7, 2024

When writing a Python application, you will likely pull in a number of third party libraries, typically installed by a package manager such as pip or conda. The application will typically depend on a specific version of each library (or a narrow range of versions). This can cause conflicts if other Python applications on your computer require different versions of the same package.

The solution to this problem is to create a separate installation space for all of the libraries, specific to the application that uses them. This is called a virtual environment.

Prior to v3, Python had a few popular third party frameworks for creating virtual environments. However, since Python 3, virtualenv is now bundled with Python and has become the de facto standard.

virtualenv

Installation

Started at Python 3, virtualenv is installed as venv with your Python installation. For Python 2.x users, you can install virtualenv with:

Terminal window
$ pip install virtualenv # Python 2.x users only

Creating a Virtual Environment

env will be the name of the virtual environment.

python -m venv env

This will create a directory called env in your current working directory.

Activating a Virtual Environment

Activating a virtual environment is necessary before you can use it. Activating it modifies your shell environment to calls to python and pip use the virtual environments installation and not any system-wide installation. Unfortunately, the way to enable it differs between Unix/macOS and Windows. You have to keep this in mind when writing code that should run on many OSes.

To activate (on UNIX or macOS):

Terminal window
$ source env/bin/activate

To activate (on Windows):

Terminal window
.venv\Scripts\activate

To deactivate (on all OSes):

Terminal window
$ deactivate

Saving Dependencies to a requirements.txt

To save all the libraries to a requirements.txt (Linux or Windows cmd.exe):

Terminal window
pip freeze > requirements.txt

Be careful if running the above command in PowerShell on Windows! By default, PowerShell will probably not use UTF-8 encoding but a non-standard UTF-16 LE. To prevent this, use the following command instead:

Terminal window
pip freeze | Out-File -Encoding UTF8 requirements.txt

Activating a Virtual Environment Inside a Batch File

If you are typing commands inside a Windows Batch file (.bat), you can’t just use the line .venv/Scripts/activate to activate the virtual environment. You will need to activate the virtual environment with call like this:

Terminal window
call .venv\Scripts\activate

pipenv

pipenv is a third-party Python virtual environment framework alternative to the built-in venv.

Installation

Terminal window
$ pip install --user pipenv