pip & Python venv Cheat Sheet
Always use a virtual environment per project so packages don't clash. On Windows, py is the Python launcher. On Linux/macOS use python3.
On this page
Create & Activate a venv #
| Task | Command |
|---|---|
| Create | |
| Create with a specific version (Windows) | |
| Activate: PowerShell | .venv\Scripts\Activate.ps1 |
| Activate: cmd | .venv\Scripts\activate.bat |
| Activate: Git Bash | |
| Activate: Linux / macOS | |
| Deactivate | deactivate |
| Check which Python is active | |
| Delete | Delete the .venv folder |
Debian/Ubuntu need the venv module first: sudo apt install python3-venv.
pip Basics #
| Command | Action |
|---|---|
| Install | |
| Exact version | |
| Version range | |
| Upgrade | |
| Remove | |
| Installed packages | |
| Packages with updates | |
| Details and location | |
| Save exact versions | |
| Install from a file | |
| Install your own project in editable mode | |
| Detect broken dependencies | |
| Clear the download cache | |
| Upgrade pip itself |
Use python -m pip instead of pip when you are unsure which Python the pip command belongs to.
Windows Python Launcher #
| Command | Action |
|---|---|
| Latest installed Python | |
| Run with a specific version | |
| Installed versions |
pipx (CLI tools globally, isolated) #
| Command | Action |
|---|---|
| Install a CLI tool in its own environment | |
| Installed tools | |
| Update all | |
| Run once without installing |
uv (fast, modern alternative) #
| Command | Action |
|---|---|
New project with pyproject.toml | |
Add a dependency (creates .venv automatically) | |
| Remove | |
| Install everything from the lockfile | |
| Run inside the project's environment | |
| Create a venv | |
| pip-compatible install (much faster) | |
| Install a Python version | |
| Run a tool once (like pipx run) |
Minimal pyproject.toml #
[project]
name = "my-app"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = [
"requests>=2.32",
]
Common Errors #
| Error | Fix |
|---|---|
externally-managed-environment (Debian/Ubuntu) | Use a venv or pipx, not the system Python |
Activate.ps1 cannot be loaded | Set-ExecutionPolicy -Scope CurrentUser RemoteSigned |
ModuleNotFoundError after installing | The venv is not active, or the editor uses another interpreter (VS Code: Python: Select Interpreter) |
| not found | Use / |
Tips #
- Add
.venv/to.gitignore. Commitrequirements.txtorpyproject.toml+ lockfile instead.
python -m venvCreate a virtual environment. python -m venv [options] folder
| Option | What it does |
|---|---|
.venv | Create it in .venv (the usual name) |
--prompt myapp | Name shown in the prompt |
--system-site-packages | Also see packages installed system-wide |
--upgrade-deps | Upgrade pip in the new venv |
--clear | Empty an existing venv first |
--without-pip | Don't install pip |
Activate: source .venv/bin/activate (Linux/macOS) · .venv\Scripts\Activate.ps1 (PowerShell) · source .venv/Scripts/activate (Git Bash). Leave with deactivate.
pyPython Launcher for Windows. py [-version] [options] [file]
| Option | What it does |
|---|---|
py | Latest installed Python |
-3.12 | A specific version (-3 = latest 3.x) |
--list (-0) | Installed versions (-0p = with paths) |
-3.12 -m venv .venv | Create a venv with that version |
-m pip install x | pip for the default Python |
#! python3.11 | First line of a script picks the version |
sourceRun a script in the current shell, so its variables, aliases and functions stay. source file [args]
| Usage | What it does |
|---|---|
source ~/.bashrc | Reload your Bash settings after editing them |
. ~/.bashrc | Same: . is the POSIX spelling of source |
source .venv/bin/activate | Activate a Python virtual environment |
source .env | Load KEY=value lines as shell variables |
set -a; source .env; set +a | …and export them to programs you start |
pythonalso: python3Run Python. python [options] [file | -m module | -c code] [args]
| Option | What it does |
|---|---|
-V (--version) | Version |
script.py | Run a file |
-m module | Run a module: -m venv, -m pip, -m http.server 8000, -m json.tool |
-c "code" | Run a code string |
-i script.py | Run, then stay in the interactive shell |
-u | Unbuffered output (logs show up immediately) |
-X dev | Development mode: extra warnings and checks |
-W error | Turn warnings into errors |
-O | Skip assert statements |
-q | No banner in the interactive shell |
-B | Don't write .pyc files |
-c "import sys; print(sys.executable)" | Which Python is this? |
pip installInstall packages. pip install [options] packages
| Option | What it does |
|---|---|
requests | Latest version |
requests==2.32.3 | Exact version (>=2,<3 for a range, quote it) |
-U (--upgrade) | Upgrade |
-r requirements.txt | Everything listed in a file |
-e . | Your own project in editable mode |
".[dev]" | Project with an optional extra |
--user | Install for your user only (outside a venv) |
--no-deps | Don't install dependencies |
--force-reinstall | Reinstall even if up to date |
--pre | Allow pre-release versions |
--no-cache-dir | Don't use / fill the cache (Docker images) |
-t folder | Install into a folder |
-c constraints.txt | Limit versions without installing |
--dry-run | Show what would be installed |
--upgrade pip | Upgrade pip itself (python -m pip install --upgrade pip) |
pip uninstallRemove packages. pip uninstall [options] packages
| Option | What it does |
|---|---|
-y | Don't ask |
-r requirements.txt | Remove everything in a file |
pip listInstalled packages. pip list [options]
| Option | What it does |
|---|---|
-o (--outdated) | Packages with newer versions |
-u (--uptodate) | Packages that are up to date |
--not-required | Packages nothing else depends on |
--format=freeze / json | Output format |
-e | Editable installs only |
--user | Only user-site packages |
pip showDetails of an installed package. pip show [options] packages
| Option | What it does |
|---|---|
requests | Version, location, dependencies ("Requires") and dependents ("Required-by") |
-f (--files) | Also list the installed files |
pip freezeInstalled packages in requirements format. pip freeze [options]
| Option | What it does |
|---|---|
> requirements.txt | Save exact versions |
--exclude-editable | Skip editable installs |
-l (--local) | Only this environment (skip global packages) |
--all | Include pip, setuptools, wheel |
pip checkCheck that installed packages have compatible dependencies. Prints problems, or "No broken requirements found".
| Usage | What it does |
|---|---|
pip check | Check the current environment |
pip cacheManage pip's download cache. pip cache command
| Command | What it does |
|---|---|
dir | Where the cache is |
info | Size and number of files |
list | Cached wheels (list requests) |
remove requests | Remove matching entries |
purge | Empty the cache |
pipalso: python -m pip py -m pipPython package installer. pip command [options] (prefer python -m pip when unsure which Python is used)
| Option | What it does |
|---|---|
--version (-V) | pip version and which Python it belongs to |
help install | Help for a command |
-q / -v | Quieter / more output |
--proxy http://host:port | Use a proxy |
--index-url URL | Another package index |
--require-virtualenv | Refuse to run outside a venv |
config list | pip settings |
pipxInstall Python command-line tools, each in its own environment. pipx command [options]
| Command | What it does |
|---|---|
install black | Install a tool globally (isolated) |
install black==24.1.0 | Specific version |
run cowsay hi | Run once without installing |
list | Installed tools |
upgrade black / upgrade-all | Update one / all |
uninstall black | Remove |
inject black click | Add a package to a tool's environment |
ensurepath | Add pipx's bin folder to your PATH |
reinstall-all | Reinstall after a Python upgrade |
uvVery fast Python package and project manager. uv command [options]
| Command / option | What it does |
|---|---|
init myapp | New project with pyproject.toml (--lib, --app, --package) |
add requests | Add a dependency (--dev, --group docs, --optional extra) |
remove requests | Remove a dependency |
sync | Install exactly what the lockfile says (--frozen in CI) |
lock | Update uv.lock (--upgrade / -U = newest versions) |
run main.py | Run in the project environment (uv run pytest) |
run --with rich script.py | Run with an extra package, temporarily |
venv | Create .venv (--python 3.12) |
pip install -r requirements.txt | pip-compatible commands (pip list, pip freeze, pip compile) |
python install 3.12 | Download and install Python versions (python list) |
python pin 3.12 | Pin the version for this project (.python-version) |
tool install ruff | Install a CLI tool (like pipx) |
tree | Dependency tree |
export --format requirements-txt | Export the lockfile |
self update | Update uv |
cache clean | Clear the cache |
uvxRun a Python tool once in a temporary environment (shortcut for uv tool run). uvx [options] tool [args]
| Usage / option | What it does |
|---|---|
uvx ruff check | Run ruff without installing it |
uvx black@24.1.0 . | Specific version |
--from httpie http | Tool whose command name differs from the package |
--with plugin | Add an extra package |
--python 3.12 | Python version to use |
Nothing on this sheet matches “”.