20.22 Python
Use a current version of Python, but not a pre-release version. See status of Python versions.
Python packages
- Poetry is recommended for package management.
- Guide for Python Package Development
- pyOpenSci provides a useful guide.
Virtual environments
Virtual environments (venv) are required to run Python code.
# sudo apt-get install python3-venv
python -m venv venv
source venv/bin/activate
# python -m pip install --upgrade pip
# python -m pip install -e .
deactivate
Dependencies
If the Python code is not managed as a package, a requirements.txt file should be created.
Linter
Black and pylint are recommended linters. They should be used part of the .pre-commit-config.yaml
:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
exclude: status.txt|readme.md
- id: end-of-file-fixer
exclude: bib$|txt$
- id: check-docstring-first
- id: debug-statements
- id: name-tests-test
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.4.2
hooks:
- id: black
language_version: python3
- repo: https://github.com/PyCQA/autoflake
rev: v2.3.1
hooks:
- id: autoflake
- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
additional_dependencies: [flake8-typing-imports==1.12.0]
args: ['--max-line-length=110', '--extend-ignore=E203,TYP006']
- repo: https://github.com/asottile/reorder-python-imports
rev: v3.13.0
hooks:
- id: reorder-python-imports
args: [--py3-plus]
- repo: https://github.com/asottile/pyupgrade
rev: v3.16.0
hooks:
- id: pyupgrade
args: [--py36-plus, --keep-runtime-typing]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.10.0'
hooks:
- id: mypy
args: [--disallow-untyped-defs, --disallow-incomplete-defs, --disallow-untyped-calls]
additional_dependencies: [types-toml]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.8
hooks:
- id: ruff # runs faster than pylint
args: [--fix, --exit-non-zero-on-fix]
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
files: colrev
args:
[
"-rn", # Only display messages
"-sn", # Don't display the score
]