Skip to main content\(\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\bivec}[1]{\overset{\curvearrowleft}{#1}}
\newcommand{\proofmark}{\class{twemoji-proofmark}{\text{M}}}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 6.2 Python Package Management
Update pip requirements.txt.
pip install -r requirements.txt --upgrade
pip freeze > requirements.txt
Pip vs uvicorn.
Table 6.2.1. Package Installation
| Install a package |
pip install requests
|
uv add requests
|
uv pip install
requests
|
| Install from requirements |
pip install -r
requirements.txt
|
uv add -r
requirements.txt
|
uv pip install -r
requirements.txt
|
| Install editable |
pip install -e .
|
uv sync
|
uv pip install -e
.
|
| Upgrade a package |
pip install --
upgrade requests
|
uv lock --upgrade-
package requests &&
uv sync
|
uv pip install --
upgrade requests
|
| Uninstall |
pip uninstall
requests
|
uv remove
requests
|
uv pip install uninstall
requests
|
Table 6.2.2. Inspecting Packages
| List packages |
pip list
|
uv pip list
|
| Show package info |
pip show requests
|
uv pip show requests
|
| Check for conflicts |
pip check
|
uv pip check
|
| Freeze to file |
pip freeze >
requirements.txt
|
uv pip freeze
or
uv export
|
Table 6.2.3. Package Installation
| Compile a lockfile |
pip-compile
requirements.in
|
uv lock
or
uv pip compile
requirements.in
|
| Install from a lockfile |
pip-sync requirements.txt
|
uv sync
|
| CI (fail if stale) |
|
uv sync --frozen
|
| Upgrade all deps |
pip-compile --upgrade
|
uv lock --upgrade && uv
sync
|
Table 6.2.4. Virtual Environments
Create venv |
python -m venv .venv
|
uv lock
or automatic with
uv pip compile
|
| Activate |
source .venv/bin/activate
|
Not needed, can use
uv run
|
| Run a script |
python script.py
(in venv) |
uv run script.py
|
| Run a tool |
python -m pytest
|
uv run pytest
|
Table 6.2.5. Python Version Management
| Install a version |
pyenv install 3.12
|
uv python install 3.12
|
| Pin to a version |
pyenv local 3.12
|
uv python pin 3.12
|
| List versions |
pyenv versions
|
uv python list
|
Table 6.2.6. CLI Tools (one-off/isolated)
| Install permanently |
pipx install ruff
|
uv tool install ruff
|
| ONe-off run |
pipx run ruff check .
|
uvx ruff check .
|
| Upgrade |
pipx upgrade ruff
|
uv tool upgrade ruff
|
| List installed |
pipx list
|
uv tool list
|
Table 6.2.7. Project Scaffolding
| New project |
python -m venv
and manual
pyproject.toml
|
uv init my-project
|
| Dev dependencies |
pip install -r
requirements-dev.txt
|
uv add --dev pytest ruff
|
| Production install (skip dev) |
Manual |
uv sync --no-dev
|