Skip to main content

Virtual Environments

Isolating Your Project's Dependencies

· 3 min read

In the previous unit, we learned to share code through GitHub. Now let's talk about managing the packages your code depends on.

Virtual Environments

Why Virtual Environments Matter

When you install a Python package with pip, it goes into your system's global Python installation. This works fine for one project, but problems arise when you have multiple projects with different requirements.

Say Project A needs requests version 2.25 and Project B needs version 2.28. With a global installation, you can only have one version installed at a time. Updating for Project B might break Project A.

Virtual environments solve this by creating isolated Python installations for each project. Each environment has its own packages, independent of other projects and the system Python. You can have different versions of the same package in different environments without conflicts.

Creating a Virtual Environment

Python includes the venv module for creating virtual environments. Navigate to your project folder and run:

python -m venv venv

This creates a venv directory containing a standalone Python installation. The directory includes its own python executable and pip.

Activating the Environment

Before installing packages, activate the environment:

# On macOS/Linux
source venv/bin/activate

# On Windows
venv\Scripts\activate

Your terminal prompt changes to show the active environment name, usually (venv) at the start. All pip install commands now install to this environment only.

To deactivate and return to your system Python:

deactivate

Installing Packages with pip

With the environment active, install packages using pip:

pip install requests

You can specify versions:

pip install requests==2.25.1    # Exact version
pip install requests>=2.25 # Minimum version

To see what's installed:

pip list

Tracking Dependencies

Other developers (or future you) need to know which packages your project requires. The convention is a requirements.txt file.

Generate one from your current environment:

pip freeze > requirements.txt

This creates a file listing every package and its version:

certifi==2021.10.8
charset-normalizer==2.0.7
idna==3.3
requests==2.25.1
urllib3==1.26.7

Someone else can recreate your environment by running:

pip install -r requirements.txt

Project: Isolate Your Turtle Project

Let's create a virtual environment for the turtle-shapes project from earlier units.

Navigate to your project directory and create the environment:

cd path/to/turtle-shapes
python -m venv venv

Activate it:

source venv/bin/activate  # macOS/Linux

If your project uses any external packages, install them:

pip install requests  # or whatever packages you need

Generate the requirements file:

pip freeze > requirements.txt

Add requirements.txt to Git, but add venv/ to your .gitignore file. The virtual environment folder is large and machine-specific; others will recreate it from requirements.txt.

echo "venv/" >> .gitignore
git add requirements.txt .gitignore
git commit -m "Add requirements.txt and ignore venv"

Now your project has documented dependencies that anyone can install.

In the next unit, we'll look at Poetry, a tool that simplifies dependency management and project configuration.