The Problem: Dependency Conflicts

Imagine you have two Python projects:

  • Project A requires version 1.0 of a library called cool-lib.
  • Project B, a newer project, requires version 2.0 of cool-lib.

If you install libraries globally (the default behavior), you can only have one version of cool-lib installed at a time. If you install v2.0 for Project B, you might break Project A! This is often called "dependency hell."

The Solution: Virtual Environments

A virtual environment is an isolated directory that contains a specific version of Python plus all the libraries required for a particular project. It's like giving each project its own clean, private workspace.

When you activate a virtual environment, your system's python and pip commands point to the versions inside that environment, not the global ones. This solves the dependency conflict problem entirely.

Using venv (The Standard Tool)

venv is the standard module for creating virtual environments and is included with Python 3.3+.

Step 1: Create the Environment

Navigate to your project folder in your terminal and run the following command. It's common practice to name the environment folder venv.

Bash


# In your project directory (e.g., my-cool-project/)
python3 -m venv venv

This creates a venv folder containing a copy of the Python interpreter and its standard libraries. You should add this venv folder to your .gitignore file.

Step 2: Activate the Environment

You must "activate" the environment to start using it. The command differs slightly by operating system.

  • macOS / Linux:
  • Bash

source venv/bin/activate
  • Windows (Command Prompt):
  • Bash

venv\Scripts\activate.bat
  • Windows (PowerShell):
  • Bash

venv\Scripts\Activate.ps1

After activation, you'll see the environment's name in your terminal prompt, like (venv) C:\Users\You\my-cool-project>.

Step 3: Deactivate the Environment

When you're finished working, simply type:

Bash


deactivate

Dependency Management with pip and requirements.txt

Once your environment is active, you use pip to install packages. These packages will be installed only inside the active environment.

Bash


# With (venv) active...
pip install requests
pip install numpy

To make your project reproducible for others (or for yourself later), you should lock your dependencies into a file. The standard convention is to use a file named requirements.txt.

Generating requirements.txt

This command lists all the packages installed in the current environment and saves them to the file.

Bash


pip freeze > requirements.txt

Your requirements.txt will look something like this:

certifi==2024.2.2
charset-normalizer==3.3.2
idna==3.6
requests==2.31.0
urllib3==2.1.0

Installing from requirements.txt

When another developer gets your project, they can create their own virtual environment, activate it, and install all the exact same dependencies with one command:

Bash


pip install -r requirements.txt

This makes project setup reliable and consistent.