Virtual Environments and Dependency Management
Overview
Virtual environments let you create isolated spaces for different projects, preventing version conflicts between libraries. You'll practice creating environments using venv and managing dependencies for organized, reproducible development, ensuring your projects work consistently across different machines.
What You Will Learn in This Lesson
By the end of this lesson, you will know:
- What virtual environments are: Understand isolated Python environments for projects.
- Creating environments: Learn how to create virtual environments using venv.
- Activating environments: Discover how to activate and deactivate virtual environments.
- Dependency management: Understand how to use requirements.txt for reproducible projects.
Why This Matters
Different projects might need different versions of the same package. Without virtual environments, installing a package for one project could break another project that needs a different version. Virtual environments solve this by giving each project its own isolated space with its own packages. This is essential for professional Python development!
Step 1: What Is a Virtual Environment?
A virtual environment is an isolated Python environment that has its own packages and dependencies. Think of it as a separate room for each project:
# Project A needs requests version 2.25.1
project_a/venv/
└── requests 2.25.1
# Project B needs requests version 2.31.0
project_b/venv/
└── requests 2.31.0
# Both can coexist without conflicts!
Isolation
Each virtual environment has its own Python interpreter and packages. Packages installed in one environment don't affect others.
Version Control
Different projects can use different versions of the same package without conflicts. This prevents "dependency hell"!
Reproducibility
You can share your project's exact dependencies with others, ensuring everyone has the same environment.
Key Concept: Virtual environments are like separate workspaces. Instead of installing all packages globally (which can cause conflicts), each project gets its own isolated environment. This is a professional Python practice!
Step 2: Creating a Virtual Environment
Python 3.3+ includes the venv module for creating virtual environments:
# Create a virtual environment
python -m venv venv
# Or with python3 on Mac/Linux
python3 -m venv venv
# This creates a 'venv' folder with isolated Python
Run Command
Run python -m venv venv in your project directory. This creates a folder called venv with an isolated Python environment.
Environment Created
The venv folder contains Python and a place for packages. It's isolated from your system Python.
Activate It
Before using the environment, you need to activate it. This switches your terminal to use the environment's Python.
Best Practice
Always create a virtual environment for each project. This keeps dependencies isolated and makes your projects more reliable. Many developers create a virtual environment as the first step when starting a new project!
Step 3: Activating and Deactivating
Once created, you need to activate the virtual environment:
# Windows
venv\Scripts\activate
# Mac/Linux
source venv/bin/activate
# After activation, your prompt shows (venv)
# Now pip installs packages only in this environment
# To deactivate
deactivate
Activate
Run the activation command for your operating system. Your terminal prompt changes to show (venv).
Install Packages
When activated, pip install installs packages only in this environment, not globally.
Deactivate
Run deactivate to exit the virtual environment and return to your system Python.
Step 4: Dependency Management with requirements.txt
You can save your project's dependencies to a file:
# Save installed packages to requirements.txt
pip freeze > requirements.txt
# Install from requirements.txt
pip install -r requirements.txt
# Example requirements.txt:
# requests==2.31.0
# numpy==1.24.3
# pandas==2.0.3
Why requirements.txt?
The requirements.txt file lists all packages your project needs. This makes it easy to:
- Share your project with others
- Set up the same environment on different machines
- Remember what packages you need
- Ensure everyone uses the same versions
End-of-Lesson Exercises
Exercise 1: Understanding Virtual Environments
Create a comment explaining what a virtual environment is and why it's useful. Then print a message explaining how virtual environments help manage project dependencies.
Write comments explaining virtual environments, then print an explanation.
Exercise 2: Dependency Management Concepts
Create comments explaining what pip freeze > requirements.txt does and what pip install -r requirements.txt does. Then print a message explaining how requirements.txt helps with project reproducibility.
Write comments explaining the commands, then print an explanation.