Managing Python packages

When working on a Python project, you will likely have external dependencies (i.e. libraries and modules) to install and use in your code. You will also want some level of isolation between the modules you need for a project, which perhaps require some version constraints, and those installed globally on your machine. We address these needs with venv and pip.

Venv

Venv is the standard Python module for creating self-contained environments and managing separate package installations for distinct projects, so that different versions of the same packages across projects don’t interfere with one another.

The Python Packaging official documentation recommends to always use a virtual environment when developing Python apps.

To create a virtual environment with Python 3.8 for a given project, do the following:

cd <project_directory>
# Create a virtual environment called my_env.
python3.8 -m venv my_env

Once you’ve created the virtual environment, you can activate it by doing the following on Linux/MacOS:

# Activate the environment on Linux/MacOS.
source my_env/bin/activate

On Windows, you activate the virtual environment as follows:

# Activate the environment on Windows.
.\my_env\Scripts\activate

To deactivate a virtual environment, simply run deactivate in your terminal’s session.

At this point, your console prompt should be prefixed with something like (my_env). You might note that, within the environment, the python, python3 and python3.8 are all alias for the Python 3.8 interpreter. On Windows, you might additionally be able to use py.

Pip

Pip (acronym for “Pip Installs Packages”) is the package installer for Python, officially endorsed and recommended by the PyPA, the Python Package Authority. It fetches and installs any package on PyPI (the Python Package Index) from source (i.e. compiling it after fetching the source) or from wheel, the current standard for Python binaries.

After you create a virtual environment, you usually want to ensure you are using the latest version of pip. To do so, run pip install --upgrade pip. Installing a package is then as easy as running pip install <package_name> e.g. pip install flask. The uninstall subcommand can likewise be used to uninstall a package: pip uninstall flask.

In order to get a snapshot of your current project’s dependencies, run pip freeze. After installing Flask as per the example above, pip freeze should produce the following:

click==7.1.2
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
Werkzeug==1.0.1

This is a list of modules including Flask itself and the packages it requires in turn to work properly. To ensure dependencies’ consistency during the lifetime of your project, you’d ideally want to save this snapshot somewhere, so that contributors to the project can always install the right version of each required package. You can achieve this by dumping the output of pip freeze into a file, conventionally called requirements.txt, which should be included in your project’s VCS repository.

pip freeze > requirements.txt

To install all the packages listed in a requirements.txt file, run pip install -r requirements.txt.

To summarise

Python projects often rely on libraries and packages that need to be installed somewhere. This place is a virtual environment which you have to create and activate upon creation of a new project. Packages can then be installed via pip.

cd <project_directory>
python3.8 -m venv my_env
source my_env/bin/activate
pip install --upgrade pip
# if a requirements.txt file exists, DO: ####
pip install -r requirements.txt
# else install packages directly
# and then dump them into a requirements.txt:
pip install <package>
pip freeze > requirements.txt
#############################################

TIP: whenever you install a new package within a virtual environment, the files of that package are in all effects places within the virtual environment folder. You therefore should not push the virtual environment folder to your VCS (e.g. git) repository. To make your life easy, add a line with the name of your virtual environment to your project’s .gitignore file e.g if your virtual environment is called my_env, run echo "my_env/" >> .gitignore.

References

This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.