Back to List

Managing Python Packages with pip and Virtual Environments

How to use pip install, venv, conda, and requirements.txt. A Python environment management guide for researchers.

Beginner
|
45min
|
Verified (2026-06)
pipVirtual EnvironmentvenvcondaPackagerequirements.txt
Progress0/7 (0%)

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 DoPackageAnalogy
Process tabular datapandasPython version of Excel
Draw graphsmatplotlibPaper figure generator
Math/statisticsnumpy, scipyStatistics software
DNA/protein analysisbiopythonSequence analysis toolkit
HTTP requestsrequestsFor 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:

bash
# Install a package
pip install pandas
# Install a specific version
pip install pandas==2.2.0
# Install multiple packages at once
pip install numpy matplotlib scipy
# List installed packages
pip list
# Uninstall a package
pip uninstall pandas

In Google Colab, prefix with !: !pip install biopython

import: Using Installed Packages

Installed packages are loaded into your code with import:

python
import numpy as np
import pandas as pd
import 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.01

as 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:

bash
# 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 environment
pip install pandas numpy matplotlib
# 5. Deactivate (when switching to another project)
deactivate

When 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.

bash
# Save the current environment's package list to a file
pip freeze > requirements.txt

Generated requirements.txt:

text
numpy==1.26.4
pandas==2.2.0
matplotlib==3.8.3
scipy==1.12.0

When a colleague receives this file:

bash
# After creating and activating a virtual environment
pip install -r requirements.txt

One 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:

bash
# Create a conda environment
conda create -n bioanalysis python=3.12
# Activate the environment
conda activate bioanalysis
# Install packages (bioconda channel)
conda install -c bioconda biopython samtools
# List environments
conda env list
# Export environment (for reproduction)
conda env export > environment.yml
pip + venvconda
InstallsPython packages onlyPython + C/R + binary tools
Env managementSeparate (venv)Integrated (conda create)
Bio toolsLimitedRich via bioconda channel
SizeLightweightHeavy (Miniconda recommended)
Recommended forWeb dev, light analysisBioinformatics, 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.

Fill in the Blanksbash
# Create a virtual environment
python3 -m .venv
# Activate (Mac/Linux)
.venv/bin/activate
# Install packages
pip pandas numpy
# Save current environment
pip 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.

๐Ÿ’ฌ Questions & Comments

0 comments

You can post without signing in. Guest comments cannot be edited or deleted by their author.

0/2000

Loading...