← Back to Course Index
Lesson 7 of 10

Virtual Environments & pip

~15 min · venv, pip, requirements.txt, the Python ecosystem

Ref
Primary Source
Official Docs — Virtual Environments and Packages (section 12)

The canonical guide to Python's package management. Short and essential — understand this before installing any third-party library.

1 — The Ecosystem Map (JS → Python)

JavaScriptPython equivalentNotes
npm / yarnpipPackage manager
node_modules/venv/Per-project packages
package.jsonrequirements.txt or pyproject.tomlDependency list
package-lock.jsonrequirements-lock.txt / pip freezePinned versions
npm installpip install packageInstall package
npxpipxRun CLI tools without installing globally
npmjs.compypi.orgPackage registry
nvmpyenvManage multiple Python versions

2 — Why Virtual Environments?

⚠ Never install packages globally

Unlike Node.js where node_modules/ lives per-project by default, Python installs packages globally by default. This causes version conflicts across projects. Always create a virtual environment first.

A virtual environment is a self-contained directory with its own Python interpreter and packages — isolated from the system Python. It's the equivalent of node_modules/ but you have to set it up manually.

3 — Creating & Activating a venv

# 1. Create a virtual environment (once per project)
python3 -m venv venv        # creates a 'venv/' folder

# 2. Activate it (every new terminal session)
source venv/bin/activate    # macOS / Linux
venv\Scripts\activate       # Windows

# Your prompt changes: (venv) $

# 3. Install packages (now goes into venv, not global)
pip install requests flask

# 4. Save your dependencies
pip freeze > requirements.txt

# 5. Deactivate when done
deactivate
# Someone else sets up your project:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt   # install all saved deps
💡 Add venv/ to .gitignore

Never commit the venv/ directory. Add it to .gitignore — just like you'd ignore node_modules/. Commit requirements.txt instead.

4 — pip Cheat Sheet

# Install
pip install requests                   # latest version
pip install requests==2.31.0           # pinned version
pip install "requests>=2.28,<3"        # version range
pip install -r requirements.txt        # install from file
pip install -e .                       # editable install (dev mode)

# Uninstall
pip uninstall requests

# List & inspect
pip list                               # all installed packages
pip show requests                      # info about a package
pip outdated                           # which packages have updates

# Upgrade
pip install --upgrade requests
pip install --upgrade pip              # upgrade pip itself

# Freeze (export current state)
pip freeze > requirements.txt
pip freeze | grep -v "^-e" > requirements.txt  # exclude editable installs

5 — pyproject.toml (Modern Standard)

The modern alternative to requirements.txt — closer to package.json. Tools like Poetry, Hatch, and uv use this.

[project]
name = "my-app"
version = "1.0.0"
description = "My Python application"
requires-python = ">=3.11"
dependencies = [
    "requests>=2.28",
    "flask>=3.0",
]

[project.optional-dependencies]
dev = [
    "pytest>=7.0",
    "black",
    "ruff",
]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

6 — Modern Tooling: uv

uv is a new, extremely fast Python package manager (written in Rust). It's becoming the recommended tool for 2024+.

# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh

# Create project (like npm init)
uv init my-project
cd my-project

# Add dependencies (like npm install package)
uv add requests flask

# Run a script in the project's venv (auto-created)
uv run python script.py

# Sync dependencies from lock file
uv sync
💡 uv vs pip

uv is 10–100x faster than pip and handles virtual environments automatically. For new projects, consider using uv — but know pip and venv as the baseline since you'll see them everywhere.

7 — Essential Third-Party Libraries

CategoryLibraryJS equivalent
HTTP clientrequests, httpxfetch, axios
Web frameworkFlask, FastAPIExpress, Hono
Data validationPydanticZod
Env variablespython-dotenvdotenv
TestingpytestJest, Vitest
LintingruffESLint
Formattingblack, ruff formatPrettier
CLI buildingClick, TyperCommander.js

🧠 Quiz

1. What command activates a virtual environment on macOS?

2. What does pip freeze > requirements.txt do?

3. Which Python tool is most equivalent to npx?

4. FastAPI is the Python equivalent of which JS library?

0/4

Questions answered correctly.