Installing and Managing Packages in Virtual Environments
You'll learn how to install Python packages safely within a virtual environment and confirm they remain isolated from your system-wide Python installation.
The Core Idea
Once you've created and activated a virtual environment (as covered in lesson 2040), you need to populate it with the packages your project requires. Installing packages inside an activated virtual environment ensures they're project-specific rather than global. This is like stocking a dedicated kitchen for one restaurant instead of mixing ingredients from multiple restaurants in a shared pantry.
Installing Packages with pip
When your virtual environment is active, using pip installs packages only into that environment:
pip install pandas
pip install numpy scikit-learn matplotlib
Each package gets downloaded and installed into your virtual environment's isolated site-packages directory, not your system Python.
Verifying Isolation
To confirm your packages are truly isolated, check what's installed inside your virtual environment:
pip list
This shows only packages in the active environment. Now deactivate and check again:
deactivate
pip list
You'll see a different list—your global Python packages. The packages you just installed won't appear here, proving isolation.
You can also verify the installation location directly:
pip show pandas
The Location: field should point to your virtual environment's directory, not your global Python installation.
Managing Packages
Update packages within the environment:
pip install --upgrade pandas
Remove packages when no longer needed:
pip uninstall numpy
Key Takeaway: Installing packages with pip inside an activated virtual environment keeps dependencies isolated and project-specific, preventing conflicts with other projects or your system Python—verify isolation by comparing pip list output when activated versus deactivated.