Managing Python Packages with pip and Virtual Environments
After Completing This Topic
You'll be able to install packages with pip, separate project environments with virtual environments (venv), and reproduce environments with requirements.txt.
Why You Need Packages
While Python's built-in features can do a lot, functions commonly needed for bio data analysis have already been built by other developers:
| What You Want to Do | Package | Analogy |
|---|---|---|
| Process tabular data | pandas | Python version of Excel |
| Draw graphs | matplotlib | Paper figure generator |
| Math/statistics | numpy, scipy | Statistics software |
| DNA/protein analysis | biopython | Sequence analysis toolkit |
| HTTP requests | requests | For calling the NCBI API |
Just as you order reagents from a catalog rather than synthesizing them each time, you install packages with pip instead of building them yourself.
pip: Installing Packages
pip is Python's package manager. Run it from the terminal:
# Install a packagepip install pandas
# Install a specific versionpip install pandas==2.2.0
# Install multiple packages at oncepip install numpy matplotlib scipy
# List installed packagespip list
# Uninstall a packagepip uninstall pandasIn Google Colab, prefix with !: !pip install biopython
import: Using Installed Packages
Installed packages are loaded into your code with import:
import numpy as npimport pandas as pdimport matplotlib.pyplot as plt
data = np.array([1.2, 3.5, 2.8, 4.1, 3.9])mean_val = np.mean(data)print(f"Mean OD value: {mean_val:.2f}")
assert abs(mean_val - 3.1) < 0.01as np is an alias. Instead of numpy.mean(), you can write np.mean() for short. np, pd, plt are conventional aliases used by developers worldwide.
Why You Need Virtual Environments
What if Project A needs pandas 1.5 and Project B needs pandas 2.2? You can't install two versions in the same Python.
Lab analogy โ each experiment uses a different set of reagents. Mixing Western Blot reagents and ELISA reagents in the same cabinet causes confusion. Separating them by experiment keeps things clean.
Virtual environment = a separate reagent cabinet for each project
venv: Creating a Virtual Environment
Use Python's built-in venv module:
# 1. Create a virtual environment (inside your project folder)python3 -m venv .venv
# 2. Activate the virtual environment# Mac/Linux:source .venv/bin/activate# Windows:.venv\Scripts\activate
# 3. Confirm activation โ (.venv) appears before the prompt(.venv) $ which python# โ /project-path/.venv/bin/python
# 4. Install packages in this environmentpip install pandas numpy matplotlib
# 5. Deactivate (when switching to another project)deactivateWhen you pip install while activated, packages are installed only in that virtual environment. Other projects are not affected.
requirements.txt: Reproducing Environments
"It works on my machine but not on my colleague's" โ the most common problem in development. requirements.txt solves it.
# Save the current environment's package list to a filepip freeze > requirements.txtGenerated requirements.txt:
numpy==1.26.4
pandas==2.2.0
matplotlib==3.8.3
scipy==1.12.0When a colleague receives this file:
# After creating and activating a virtual environmentpip install -r requirements.txtOne command reproduces the exact same package environment. It's the same principle as writing reagent catalog numbers in your experiment protocol. The key to reproducibility.
conda: A Package Manager for Scientific Research
conda is similar to pip but can manage not only Python packages, but also C libraries and R packages. In bioinformatics, conda is used more frequently:
# Create a conda environmentconda create -n bioanalysis python=3.12
# Activate the environmentconda activate bioanalysis
# Install packages (bioconda channel)conda install -c bioconda biopython samtools
# List environmentsconda env list
# Export environment (for reproduction)conda env export > environment.yml| pip + venv | conda | |
|---|---|---|
| Installs | Python packages only | Python + C/R + binary tools |
| Env management | Separate (venv) | Integrated (conda create) |
| Bio tools | Limited | Rich via bioconda channel |
| Size | Lightweight | Heavy (Miniconda recommended) |
| Recommended for | Web dev, light analysis | Bioinformatics, large-scale analysis |
Start with pip + venv, and switch to conda when you need bio tools like samtools or BLAST.
Try It Yourself (Faded Example)
Fill in the blanks to complete the commands for creating a virtual environment and installing packages.
# Create a virtual environmentpython3 -m .venv# Activate (Mac/Linux).venv/bin/activate# Install packagespip pandas numpy# Save current environmentpip freeze > .txt
Common Errors & Solutions
Q: I get ModuleNotFoundError: No module named 'pandas'
The package isn't installed, or it's installed in a different Python environment. Check which Python you're using with which python. Also verify that you've activated your virtual environment.
Q: I ran pip install but import doesn't work
The terminal's pip and Jupyter Notebook's Python might be in different environments. In Jupyter, run !pip install pandas in a cell, then restart the kernel.
Q: What's the difference between pip and pip3?
pip is for the default Python, pip3 is specifically for Python 3. The distinction matters when both Python 2 and 3 are installed. Inside a virtual environment, pip always connects to that environment's Python, so no distinction is needed.
Q: Can I push the .venv folder to Git?
Don't. .venv contains hundreds of MB of package binaries. Add .venv/ to .gitignore and only push requirements.txt to Git. Colleagues can create their own environment from requirements.txt.