pip and Virtual Environments
The standard library covers many scenarios, but real projects almost always use third-party libraries —
requestsfor HTTP,pandasfor data,flaskfor web apps. This lesson teaches you how to install and manage these libraries, and how to use virtual environments to isolate dependencies across projects.
1. pip — Python's Package Manager
pip is Python's official package manager for installing, uninstalling, and managing third-party libraries.
# Install a package
pip install requests
# Install a specific version
pip install requests==2.31.0
# Uninstall a package
pip uninstall requests
# List installed packages
pip list
# View package info
pip show requests
pip install package_name automatically downloads and installs from PyPI.
2. requirements.txt
When your project depends on multiple third-party libraries, use requirements.txt to record them:
requests==2.31.0
flask==3.0.0
pandas==2.0.0
numpy==1.24.0
# Install all dependencies (run after cloning a project)
pip install -r requirements.txt
# Generate requirements.txt (all packages in current environment)
pip freeze > requirements.txt
Example: Calling an API with requests (Difficulty ⭐⭐)
# Install first: pip install requests
import requests
# Call a public weather API
try:
response = requests.get(
"https://api.open-meteo.com/v1/forecast",
params={
"latitude": 39.9,
"longitude": 116.4,
"current_weather": True
},
timeout=10
)
if response.status_code == 200:
data = response.json()
weather = data["current_weather"]
print(f"Beijing current temperature: {weather['temperature']}°C")
print(f"Wind speed: {weather['windspeed']} km/h")
else:
print(f"Request failed: {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"Network error: {e}")
3. Virtual Environments (venv)
Different projects may need different versions of the same package. Virtual environments solve this — each project has its own isolated environment.
# Create a virtual environment
python -m venv myenv
# Activate the virtual environment
# Windows:
myenv\Scripts\activate
# macOS/Linux:
source myenv/bin/activate
# After activation, the prompt shows (myenv) at the beginning
# Packages installed now only affect this environment
# Deactivate the virtual environment
deactivate
Workflow
# 1. Create project directory
mkdir my_project
cd my_project
# 2. Create virtual environment
python -m venv venv
# 3. Activate
# Windows:
venv\Scripts\activate
# macOS/Linux:
source venv/bin/activate
# 4. Install dependencies
pip install requests flask
# 5. Record dependencies
pip freeze > requirements.txt
# 6. Deactivate when done
deactivate
# Someone else cloning the project:
# python -m venv venv
# source venv/bin/activate (or Windows equivalent)
# pip install -r requirements.txt
4. Common Third-Party Libraries Quick Reference
| Library | Purpose | Install Command | One-Liner |
|---|---|---|---|
requests |
HTTP requests | pip install requests |
Most popular way to make HTTP requests |
flask |
Web framework | pip install flask |
Lightweight Python web framework |
pandas |
Data analysis | pip install pandas |
Excel-level data processing |
numpy |
Numerical computing | pip install numpy |
High-performance array operations |
beautifulsoup4 |
Web scraping | pip install beautifulsoup4 |
Extract data from HTML |
pillow |
Image processing | pip install pillow |
Open, edit, and save images |
pytest |
Testing framework | pip install pytest |
Most popular Python testing framework |
Example: Reading CSV with pandas (Difficulty ⭐⭐)
# Install first: pip install pandas
import pandas as pd
# Read CSV file
df = pd.read_csv("students.csv")
print(df.head()) # First 5 rows
print(f"Rows: {len(df)}, Columns: {len(df.columns)}")
print(df.describe()) # Statistical summary
Common Use Cases
- Project initialization: Create venv → install dependencies → start coding.
- Dependency management: Lock versions with
requirements.txtfor consistency across team and production. - Environment isolation: Different projects use different versions of the same library (e.g., Project A uses Flask 2.x, Project B uses Flask 3.x).
- One-click deploy:
pip install -r requirements.txtinstalls all dependencies at once.
❓ FAQ
pip install and python -m pip install?python -m pip explicitly specifies which Python interpreter's pip to use, which is safer in multi-Python-version environments. Get in the habit of using python -m pip install.
Q: Should the virtual environment be inside or outside the project directory? A: Typically inside the project root, named venv or .venv. This makes it obvious to other developers. Remember to add venv/ to .gitignore — virtual environments should not be committed to version control.
Q: pip freeze > requirements.txt lists all packages, including dependencies of dependencies. Isn't that too many? A: pip freeze does list everything. It's common to keep only the top-level packages and remove indirect dependencies. Alternatively, use pipreqs, which only scans packages that are actually imported in your code.📖 Summary
pip install package_nameinstalls third-party packages;pip uninstallremoves themrequirements.txtrecords project dependencies;pip install -r requirements.txtinstalls them all- Virtual environments (
venv) isolate dependencies for each project - Workflow: create venv → activate → pip install → develop → freeze → commit
- Common third-party libraries:
requests,flask,pandas,numpy,beautifulsoup4
📝 Exercises
-
Basic (Difficulty ⭐): Create a virtual environment, activate it, install the
requestslibrary, confirm withpip list, then deactivate. -
Intermediate (Difficulty ⭐⭐): Use the
requestslibrary to call https://api.github.com/repos/python/cpython and retrieve info about the CPython repository. Output the repo name, star count, fork count, and last update time. -
Challenge (Difficulty ⭐⭐⭐): Create a project directory, initialize a virtual environment, install
flaskandrequests, generate arequirements.txtwithpip freeze. Then delete the virtual environment and rebuild from scratch usingrequirements.txt— verify it works.



