Python Virtual Environments
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:
Creating a Virtual Environment
env
will be the name of the virtual environment.
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):
To activate (on Windows):
To deactivate (on all OSes):
Saving Dependencies to a requirements.txt
To save all the libraries to a requirements.txt
(Linux or Windows cmd.exe
):
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:
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:
pipenv
pipenv is a third-party Python virtual environment framework alternative to the built-in venv
.