Virtual Environments and pip – Managing Project Dependencies

Section Overview

When working on Python projects, you often need to install third-party packages. However, installing everything globally can cause conflicts between projects. In this section, you’ll learn how to isolate dependencies using virtual environments, and how to install and manage packages with pip.

By the end of this section, you will be able to:

  • Create and activate a virtual environment
  • Install and uninstall packages using pip
  • Use requirements.txt to document dependencies
  • Understand why isolation is important for professional development

Lesson 1: What Is a Virtual Environment?

A virtual environment is a self-contained Python environment that includes its own version of Python and its own set of installed packages. It allows you to avoid conflicts between project dependencies.


Lesson 2: Creating and Activating a Virtual Environment

In your terminal or command prompt:

# Create a new virtual environment
python -m venv venv

# On macOS/Linux: source venv/bin/activate

# On Windows:
venv\Scripts\activate

You’ll know it's activated when you see (venv) at the start of your terminal prompt.

To deactivate:

deactivate

Lesson 3: Installing Packages with pip

Once your virtual environment is activated, use pip to install external packages:

pip install requests

Check installed packages:

pip list

Uninstall a package:

pip uninstall requests

Lesson 4: requirements.txt – Documenting Your Project's Dependencies

You can freeze your current environment’s packages:

pip freeze > requirements.txt

Then, in the future or on another computer, others can recreate the same environment:

pip install -r requirements.txt

Lesson 5: Why This Matters in Real Projects

Without virtual environments, different projects on your machine can interfere with each other — for example, one may need Flask version 1.1 and another may require version 2.0. A virtual environment ensures each project has exactly what it needs.

This is also the standard in professional software development — for testing, deploying, or collaborating with teams.


Quiz: Check Your Understanding

1. What command creates a new virtual environment named venv?
Answer: python -m venv venv

2. What does pip install -r requirements.txt do?
Answer: Installs all packages listed in the file.

3. Why are virtual environments important?
Answer: They isolate project dependencies to avoid conflicts.

4. What is the purpose of requirements.txt?
Answer: It documents the packages a project depends on.


Practice Exercise: Set Up a Clean Environment

  1. Create a folder called my_project
  2. Inside it, create and activate a virtual environment
  3. Install the requests package
  4. Use pip freeze to generate a requirements.txt
  5. Deactivate the environment and test re-activating it

You can verify it works by writing a simple script using requests:

# fetch_data.py import requests

response = requests.get("https://api.github.com")
print("Status code:", response.status_code)