pip and Virtual Environments

The standard library covers many scenarios, but real projects almost always use third-party libraries — requests for HTTP, pandas for data, flask for 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.

BASH
# 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 downloads packages from PyPI (Python Package Index). PyPI is Python's official package repository, hosting over 500,000 packages. 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:

TXT
requests==2.31.0
flask==3.0.0
pandas==2.0.0
numpy==1.24.0
BASH
# 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 ⭐⭐)

PYTHON
# 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}")
▶ Try it Yourself

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.

BASH
# 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

BASH
# 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 ⭐⭐)

PYTHON
# 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
▶ Try it Yourself

Common Use Cases


❓ FAQ

Q What's the difference between pip install and python -m pip install?
A They produce the same result. 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_name installs third-party packages; pip uninstall removes them
  • requirements.txt records project dependencies; pip install -r requirements.txt installs 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

  1. Basic (Difficulty ⭐): Create a virtual environment, activate it, install the requests library, confirm with pip list, then deactivate.

  2. Intermediate (Difficulty ⭐⭐): Use the requests library 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.

  3. Challenge (Difficulty ⭐⭐⭐): Create a project directory, initialize a virtual environment, install flask and requests, generate a requirements.txt with pip freeze. Then delete the virtual environment and rebuild from scratch using requirements.txt — verify it works.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏