An example packaged python project
- Your code is a single use script with no 3rd party dependencies
- No tools needed
- Your code has 3rd party dependencies
- Package manager and virtual environments:
pip,venv,.venv,requirements.txt(orcondaandenvironment.yml)
- Package manager and virtual environments:
- Your code is not single use (e.g. others or your future self will look at it and possibly use it)
- Version control:
git,.gitignore README.md(.txt,.rst)- Testing:
tests/,pytest,doctest,unittest - Linting:
pylint;flake8 - Formatting:
isort;black - Complexity Checker:
mccabe - Type Checking:
mypy - Coverage:
coverage
- Version control:
- Several developers will be adding to the code
pre-commitand.pre-commit-config.yamltox,tox.ini- CI/CD: GitHub Actions,
.github/workflows - CodeCov
- The code will be used (i.e. installed) by other projects
pyproject.tomlsetup.cfg(setup.py?)
- The code will be publically available
LICENSE.txtMANIFEST.indocs/,pydoc,sphinx- README badges
- TestPyPI and PyPI,
twine
The following project directory structure is recommended and assumed below:
└── $PROJECT_DIR - top level project directory
├── src
│ ├── LexPkg
│ └── ...other source code, cloned repos, etc.
├── bin - single file scripts and binaries. Good to append to $PATH
├── outputs - any output files/directories created by running programs
└── ...build, data, etc.
Instructions below assume you are inside $PROJECT_DIR
cd $PROJECT_DIR
python -m venv .venv --prompt .
source .venv/bin/activate
pip install --upgrade pip
pip install --editable src/LexPkg
pip install -r src/LexPkg/requirements.txt -r src/LexPkg/requirements-dev.txtsource .venv/bin/activateManually run tests
# PyTest
pytest tests/
# DocTest
python -m unittest -v tests/doctest_runner.py
# Coverage checker
coverage run -m pytest tests/
coverage report
# Import order checker
isort --check ./
# Linter
pylint --recursive y --ignore .venv,.tox,docs ./ | grep -o "Module.*"
flake8 --extend-exclude .venv --format quiet-filename ./
# Autoformatter
black --check ./
# Type checker
mypy --exclude docs ./
MYPYPATH=src mypy tests/ # for running on tests/ only
# Complexity checker
find ./src -name "*py" | xargs -n1 -t python -m mccabe --min 1
# Pre-commit hooks
pre-commit run --all-files
# Environment tests
tox ./To followup on a specific file
pytest path/to/file.py
python -m doctest -v path/to/file.py
coverage report -m path/to/file.py
isort --check --diff path/to/file.py
pylint path/to/file.py
flake8 path/to/file.py
black --check --diff path/to/file.py
mypy path/to/file.py
python -m mccabe path/to/file.py
# pre-commit N/ATo generate and view the documentation:
cd docs
make html
open build/html/index.htmlThis is currently not working.
Commands copied from "Packaging Python Projects"
pip install --upgrade build
python -m buildInitial upload to TestPyPI
pip install --upgrade twine
twine upload --repository testpypi dist/*
Enter your username: __token__
Enter your password: <your api token>Installing package
pip install --index-url https://test.pypi.org/simple/ --no-deps lexpkgManually publish a new release
TODO
Initial upload to PyPI
TODO