Python Quick-Ref
Managing python versions, environments and dependencies
Use uv!
"uv-first" projects
-
Initialize new project
uv init {project_name} -
Initialize existing project and install deps
uv sync -
Install and update existing deps
uv sync --upgrade -
Add dep
uv add {dep}[version_constraint] -
Remove dep
uv remove {dep} -
Upgrade dep
uv lock --upgrade-package {dep}
uv-first scripts
uv add --script example.py pandas # Legger til kommentar på toppen av filen
uv run example.py
Debugging and troubleshooting
-
Debugging pandas dataframes and series, plot pd.Series:
breakpoint() # Stop execution and enter debugger. Enter the remaining into debug console at the right time import matplotlib.pyplot as plt df.plot() plt.show() # plot appears
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 Ior 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/