This lesson on Virtual Envs & Dependency Management is hands-on and example-driven. You will create, activate, and manage isolated Python virtual environments using the built-in venv module across Windows, macOS, and Linux. You will isolate project dependencies, inspect installed libraries, and reproduce identical environments using pip and requirements files.
What You'll Be Able To Do
- Create an isolated virtual environment directory using the built-in venv module.
- Activate and deactivate virtual environments on Windows, macOS, and Linux terminals.
- Inspect and install specific packages inside an activated virtual environment using pip.
- Export exact project dependencies into a reproducible requirements.txt file.
- Replicate an existing project environment from a requirements.txt file.
Detailed Concept Walkthrough
1. Virtual Environment Concepts and Creation
A virtual environment is a self-contained directory containing an isolated Python runtime and separate library directory. It prevents dependency version collisions between multiple projects on the same machine.
- Mechanism: Running the venv module creates a standalone folder containing a local Python executable copy and dedicated site-packages directory.
- Execution Flow: The command targets a folder name (typically 'env' or 'venv') inside your project root, alongside your application code.
- Best Practice: Never put custom source code files inside the virtual environment directory itself, as the entire folder should remain disposable.
# Navigate to your project root directory
cd /path/to/project
# Create a virtual environment named 'env' (macOS/Linux)
python3 -m venv env
# Create a virtual environment named 'env' (Windows)
# python -m venv env
Key Takeaway: Virtual environments isolate package installations per project to prevent version conflicts without affecting global Python installations.
2. Environment Activation and Deactivation
Activation adjusts terminal session environment variables so that 'python' and 'pip' point directly to the isolated environment's binaries. Deactivation restores the terminal session back to the global system Python path.
- Mechanism: On Unix-like systems, sourcing bin/activate modifies the active shell session's PATH variable; on Windows, a batch script or PowerShell script performs this update.
- Visual Cue: A successful activation prepends the virtual environment's name in parentheses directly to your terminal prompt.
- Syntax Rule: Calling 'deactivate' reverts PATH changes and works identically across Windows, macOS, and Linux platforms.
# Activate on macOS / Linux
source env/bin/activate
# Activate on Windows (Command Prompt)
# env\Scripts\activate.bat
# Exit the virtual environment back to global Python
deactivate
Key Takeaway: Activating updates your current shell session PATH to point to local binaries, while deactivating restores global paths.
3. Package Management with Pip
Pip installs and manages external packages exclusively within the active virtual environment. This ensures third-party packages and transitive dependencies do not alter global system libraries.
- Mechanism: When activated, pip downloads packages and saves them into the local environment's lib directory rather than global site-packages.
- Under the Hood: Inspecting installed modules with 'pip list' inside a fresh virtual environment shows only base tools like pip and setuptools.
- Execution Flow: Installing a top-level library automatically resolves and downloads all necessary sub-dependencies into the isolated environment.
# List packages in the current active environment
pip list
# Install a specific package (e.g., requests)
pip install requests
# Verify newly installed library and its sub-dependencies
pip list
Key Takeaway: Installing packages inside an active virtual environment confines all library files and sub-dependencies strictly to that local folder.
4. Dependency Export and Replication
Requirements files allow sharing and reproducing identical runtime environments across teams and deployment environments. Pinned versions guarantee deterministic dependency resolution.
- Mechanism: 'pip freeze' outputs all installed top-level and transitive dependencies alongside their exact pinned versions.
- Best Practice: Standard Unix redirection writes this frozen list directly into a shareable requirements.txt file committed to version control.
- Execution Flow: The '-r' flag instructs pip to iterate through each entry in a requirements file and install all specified versions.
# Export all installed package versions to a file
pip freeze > requirements.txt
# In a new environment, install dependencies from file
pip install -r requirements.txt
Key Takeaway: Use pip freeze to capture environment state and pip install -r to deterministically replicate dependencies anywhere.
Topics Covered in Virtual Envs & Dependency Management
- Virtual Environments Overview (0:00 - 0:45) — Explains why project dependency isolation prevents cross-project package version conflicts.
- Creating Environments (0:45 - 1:22) — Demonstrates creating virtual environment directories using the built-in venv module across platforms.
- Activation and Deactivation (1:22 - 2:15) — Shows how to activate and deactivate virtual environments on Windows, macOS, and Linux.
- Package Management (2:15 - 3:07) — Illustrates inspecting clean environment libraries and installing external packages like requests with pip.
- Exporting and Replicating Dependencies (3:07 - 4:24) — Covers exporting package snapshots with pip freeze and reinstalling them via requirements.txt.
- Project Structure and Cleanup (4:24 - 5:30) — Outlines directory organization best practices, code separation, and environment deletion.
Python Cheat Sheet
-
python3 -m venv <name>— Creates new virtual environment folder on macOS/Linuxpython3 -m venv env -
python -m venv <name>— Creates new virtual environment folder on Windowspython -m venv env -
source <name>/bin/activate— Activates virtual environment on macOS/Linux terminalsource env/bin/activate -
<name>\Scripts\activate.bat— Activates virtual environment on Windows Command Promptenv\Scripts\activate.bat -
deactivate— Deactivates current virtual environment across platformsdeactivate -
pip list— Lists all packages installed in environmentpip list -
pip freeze > requirements.txt— Saves installed package versions to filepip freeze > requirements.txt -
pip install -r <file>— Installs all packages listed in filepip install -r requirements.txt
Comparison Table
| Action / Task | macOS & Linux | Windows |
|---|---|---|
| Create Environment | python3 -m venv env | python -m venv env |
| Activate Environment | source env/bin/activate | env\Scripts\activate.bat |
| Deactivate Environment | deactivate | deactivate |
| Delete Environment | rm -r env | Move folder to trash |
Common Pitfalls
- Mistake: Saving project source code files directly inside the virtual environment directory. Avoid: Store source files in the project root alongside the environment folder.
- Mistake: Committing the entire virtual environment folder to git repositories. Avoid: Export package versions to requirements.txt and delete or gitignore the virtual environment folder.
- Mistake: Running pip install without activating the target virtual environment. Avoid: Confirm the environment name prefix appears in your terminal prompt before running pip.
FAQs
- Why does pip list show global packages instead of isolated project packages? The virtual environment is not currently active. Run the activation script for your operating system and verify the terminal prompt shows the environment prefix.
- Can I rename or move a virtual environment directory to another folder? Virtual environment scripts contain hardcoded file paths. Delete the existing directory, re-create it in the new location, and reinstall dependencies using your requirements.txt.
- What tools other than venv exist for Python environment management? Third-party alternatives mentioned in the lesson include Conda, virtualenv, Pipenv, and Poetry, though venv is built directly into Python.
- How do I completely remove a virtual environment when finished? Deactivate the environment if active, then delete the virtual environment folder using your terminal or system file manager.