🐍
Python (venv & pip) cheat sheet
Everyday Python tooling. Always use a virtual environment per project so dependencies do not clash. On Windows, activate with .venv\Scripts\activate instead of the source command.
Click any command to copy it.
Virtual environments
| Command | What it does |
|---|---|
python3 -m venv .venv | Create a virtual environment in .venv. |
source .venv/bin/activate | Activate it (Linux/macOS). |
.venv\Scripts\activate | Activate it (Windows). |
deactivate | Leave the environment. |
which python | Confirm you are using the environment's Python. |
rm -rf .venv | Destructive: Delete the environment; recreate it from requirements.txt. |
python3 -m venv --upgrade-deps .venv | Upgrade pip inside an environment. |
pip
| Command | What it does |
|---|---|
pip install package | Install a package. |
pip install package==1.2.3 | Install an exact version. |
pip install -U package | Upgrade a package. |
pip install -r requirements.txt | Install everything in a requirements file. |
pip freeze > requirements.txt | Save the installed packages and versions. |
pip list --outdated | Show packages with newer versions. |
pip show package | Show details and installed location. |
pip uninstall package | Remove a package. |
pip install -e . | Install the current project in editable mode. |
pip cache purge | Clear pip's download cache. |
python3 -m pip install --upgrade pip | Upgrade pip itself. |
Run things
| Command | What it does |
|---|---|
python3 script.py | Run a script. |
python3 -c "print(2 + 2)" | Run a one-line command. |
python3 -m http.server 8000 | Serve the current directory over HTTP on port 8000. |
python3 -m json.tool data.json | Pretty-print a JSON file. |
python3 -m pdb script.py | Debug with the built-in debugger. |
python3 -m timeit 'sum(range(1000))' | Time a small snippet. |
python3 -m py_compile file.py | Check a file for syntax errors. |
PYTHONPATH=. python3 script.py | Add the current folder to the import path. |
Testing
| Command | What it does |
|---|---|
pytest | Run all tests. |
pytest -x | Stop at the first failure. |
pytest -k name | Run tests whose names match. |
pytest path/to/test_file.py::test_name | Run one test. |
pytest -v | Verbose output. |
pytest --lf | Re-run only the tests that failed last time. |
pytest --cov=package | Measure coverage (needs pytest-cov). |
Tools
| Command | What it does |
|---|---|
pipx install tool | Install a command-line tool in its own isolated environment. |
python3 --version | Show the Python version. |
pip config list | Show pip configuration. |
python3 -m ensurepip --upgrade | Install pip where it is missing. |
Frequently asked questions
Why should I use a virtual environment?
It keeps each project's dependencies separate, so upgrading a library for one project cannot break another, and it avoids modifying system Python packages.
What does 'externally-managed-environment' mean?
Recent Linux distributions block pip from changing the system Python. Create a virtual environment (python3 -m venv) or use pipx for command-line tools.