Python Quick-Ref
Managing python versions, environments and dependencies
Use uv!
"uv-first" projects
-
Initialize project and create venv
uv init {project_name}
-
Add dep
uv add {dep}[version_constraint]
-
Remove dep
uv remove {dep}
-
Upgrade dep
uv lock --upgrade-package {dep}
"pip-first" projects
-
Initialize venv
uv venv --python 3.12 . .venv/bin/activate
-
If relying on internal packages using Azure DevOps Artifacts, the UV_EXTRA_INDEX_URL env variable can be used to configure this. A personal access token can be included in the URL for basic auth (see uv docs for details).
For my current (2024) project I'm adjusting the venv activate script to set this environment variable when activating venv, and unset it in the deactivate function. It is also possible to add the index url to pyproject.toml, but then authorization must be handled differently, e.g. with artifacts-keyring (obviously don't check your PAT into git).
-
-
Add dep
uv pip install {dep}
Or add it to requirements.txt and
uv pip install -r requirements.txt
Or add it to pyproject.toml dependencies and
uv pip install -e .
-
Remove dep
uv pip uninstall {dep}
-
Upgrade dep
uv pip install -U {dep}
Or update version constraints in requirements.txt if needed and
uv pip install -U -r requirements.txt
Convenient cli's
-
Generate a uuid:
python -m uuid
-
Serve static files in current directory:
python -m http.server 8000
Logging in pytest output
pytest -o log_cli=true --log-level=DEBUG
or persistant in a config file, which by convention resides in the root directory of the repository
# pytest.ini
log_cli=true
log_level=DEBUG
# pyproject.toml
[tool.pytest.ini_options]
log_cli="true"
log_level="DEBUG"
Ruff
-
Format:
ruff format {file|dir}
-
Lint:
ruff check [--fix] {file|dir}
-
Organize imports:
ruff check --fix --select I
or add to pyproject.toml:[tool.ruff.lint] extend-select = ["I"]
-
Example config:
[tool.ruff] line-length = 120 [tool.ruff.lint] extend-select = ["I"] [tool.ruff.lint.isort] known-first-party = ["myorg_*"]
BasedPyright
A fork of the Pyright LSP with various feature improvements, such as code action for missing imports!
Works nicely with terminal editors like Helix.
See https://docs.basedpyright.com/latest/