Virtual Environments

These let you install Python modules in a local directory instead of system-wide. Use them!

Reasons:

  • Keep your system files clean
  • Keep projects from conflicting with each other
  • More consistent across developer setups

Create

$ python -m venv ./venv

Activate

Mac and Linux:

$ source venv/bin/activate

Windows:

$ venv/Scripts/activate.bat  # Terminal
$ venv/Scripts/Activate.ps1  # Powershell

Your terminal prompt should now have (venv) at the start. Now pip modules will install locally!

Deactivate

$ deactivate

Requirements

The best way to track dependencies is to manually create a requirements.txt file, then add all packages you install into that file first before installing them. It’s also a good idea to use the ~= “compatible release” version specifier.

Quick and dirty way:

# Dump current requirements into a file
$ pip freeze > requirements.txt

# Install requirements from file
$ pip install -r requirements.txt
Tip

Most IDEs - like PyCharm - can automatically use virtual environments and generate the requirements.txt file.

The requirements file provides an easy way to keep a list of module dependencies, and allow different developers to configure the same environment.

Sources

Last updated: 15 December 2022

Back to previous page