knowledge-kitchen

Virtual Environments (in Python)

Isolating and documenting project dependencies.

  1. Overview
  2. pip
  3. pipenv
  4. venv
  5. Using IDEs with virtual environments
  6. Conclusions

Overview

Concept

Python comes with an ability to create virtual environments - specific areas of memory and disk space where you can install the dependencies and settings necessary to run a specific app in isolation from other apps on the same computer.

Choices

There are multiple tools for setting Python virtual environments. We will focus on:

Another popular tool for the job is conda, but we will not discuss it here as it is most commonly used with the Anaconda distribution of Python, not the standard distribution.

pip

Overview

Before we discuss virtual environments, we must understand pip, the standard “package manager” used to download and install dependencies into a Python project.

Base environment

If no virtual environment is active, pip will install modules into the global base Python environment, which is shared by all projects on the computer.

Documenting dependencies

The pip freeze command is used to list all of the modules installed into the current Python environment.

pipenv

Overview

The ability to make virtual environments with pipenv is relatively easy, but it does not come pre-installed with Python. It must be installed.

Install pipenv using pip:

pip install pipenv

Activate it:

pipenv shell

Your command line is now running within a virtual environment. Installing or uninstalling any modules will only affect the current environment.

Path to environment

Generally, you don’t need to worry about where pipenv stores the virtual environment files… it just works. However, in some cases, such as setting an IDE to use a specific environment, it may be helpful to know the path to the virtual environment files.

When running pipenv shell, the path to the virtual environment is printed to the console.

pipenv shell file path

Installing dependencies

Use the pipenv install command to download and install dependencies into the current virtual environment.

pipenv install Flask
pipenv install pymongo

As you install dependencies with pipenv, you will a file named Pipfile that is automatically created (if it doesn’t yet exist) and modified to list the dependencies you’ve installed.

You may want to specify a specific version of Python to be used in this virtual environment:

For example:

pipenv --python 3.10

Configuration file

The file named Pipfile contains the list of dependencies for the current project.

Naming virtual environments

If you have many virtual environments for many projects, it can be helpful to name them intuitive mnemonic names.

In pipenv, we do this by first creating an environment variable in the command shell (Terminal) named PIPENV_CUSTOM_VENV_NAME and setting it to our desired name.

export PIPENV_CUSTOM_VENV_NAME=my_custom_venv_name

Secondly, xwe create a new virtual environment which adopts that name:

pipenv shell

venv

Overview

If you are unable or refuse to use pipenv for some reason, you can use the more traditional venv instead. The ability to make virtual environments with venv comes included with standard Python distributions.

This command creates a new virtual environment with the name .venv:

python -m venv .venv # try 'python3' instead of 'python' if your system requires it

To activate the virtual environment named .venv

On Mac:

source .venv/bin/activate

On Windows:

.venv\Scripts\activate.bat

Installing dependencies

Once a venv virtual environment has been activated, any dependencies installed with pip will be installed into the virtual environment, not the global environment.

pip install Flask
pip install pymongo

As with pip generally, you can run pip freeze to save the installed dependencies into a settings file named requirements.txt. This file can then be shared and used to install the same dependencies on other computers or in other virtual environments.

Using IDEs with virtual environments

Overview

When you have set a virtual environment using the command shell (i.e. terminal), the IDE you are using to edit the code may not be configured to use the same environment.

Any good Python IDE will allow you to select the virtual environment to use when running the code.

Visual Studio Code

For example, if you are using Visual Studio Code, you may notice that the linter in the IDE will flag code as incorrect that references modules that are indeed installed in the virtual environment. This is usually because the IDE is not set to use the same environment.

VSCode select Python interpreter

Conclusions

Thank you. Bye