The canonical guide to Python's package management. Short and essential — understand this before installing any third-party library.
| JavaScript | Python equivalent | Notes |
|---|---|---|
npm / yarn | pip | Package manager |
node_modules/ | venv/ | Per-project packages |
package.json | requirements.txt or pyproject.toml | Dependency list |
package-lock.json | requirements-lock.txt / pip freeze | Pinned versions |
npm install | pip install package | Install package |
npx | pipx | Run CLI tools without installing globally |
npmjs.com | pypi.org | Package registry |
nvm | pyenv | Manage multiple Python versions |
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.
# 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
Never commit the venv/ directory. Add it to .gitignore — just like you'd ignore node_modules/. Commit requirements.txt instead.
# 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
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"
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 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.
| Category | Library | JS equivalent |
|---|---|---|
| HTTP client | requests, httpx | fetch, axios |
| Web framework | Flask, FastAPI | Express, Hono |
| Data validation | Pydantic | Zod |
| Env variables | python-dotenv | dotenv |
| Testing | pytest | Jest, Vitest |
| Linting | ruff | ESLint |
| Formatting | black, ruff format | Prettier |
| CLI building | Click, Typer | Commander.js |
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?
Questions answered correctly.