π Modern Python project Cookiecutter template
Each time you start a new Python project, you shouldn't start from scratch. Ideally, you'd start with a standard project structure and set of tools and integrations to help facilitate writing quality Python code.
This modern Python Cookiecutter template is the tool that will help you do just that.
-
uvto manage dependencies -
structlogfor logging -
mypyfor static typing -
pytest,hypothesis,mutmutfor testingpytest-covfor coverage reportspytest-mockfor mockspytest-xdistfor distributed testingpytest-randomlyto randomly order tests
-
toxfor testing automation -
sphinxfor docsmyst-parserfor markdown docs- selectable theme
-
pdbpfor debugging -
pre-commithooks with comprehensive tooling:uv-lockfor dependency managementmypyfor type checkingrufffor linting and formattingblackfor code formattingblacken-docsfor formatting code in docsisortfor import sortingbashatefor shell script lintingcommitlintfor commit message standardsdetect-secretsfor secret detectiontyposfor spell checkingdeptryfor dependency validation
-
dockerfilefor development, testing, and production -
dunamaifor versioning -
custom
Justfile(runjust) -
stay up-to-date w/ configured
dependabot -
github-actionswith ci (leveragingtox), publish to pypi workflows w/release-drafterintegration-
- optional
codecovintegration for code coverage
- optional
-
publishworkflow with configurable publishing to:- TestPyPI (optional, enabled by default)
- PyPI (optional, enabled by default)
- GitHub Packages (optional, disabled by default)
- GitHub Release artifacts (optional, disabled by default)
- Always builds and uploads artifacts to GitHub Actions for manual inspection
- Integrated with
release-drafter
-
auto approve / mergeworkflow -
with these additional workflows:
codeqlfor security analysishadolintfor Dockerfile lintingpr-size-labelingfor PR size classificationpr-labelerfor automated PR labelingcommitlintfor commit message validationtrufflehogfor secret scanningdocsfor documentation publishingrelease-drafterfor automated release notes
-
Install Cookiecutter
uv add cookiecutter
# pip install cookiecutter
# pipenv install cookiecutterOptional: Install Cruft
uv add cruft
# pip install cruft
# pipenv install cruftcookiecutter gh:ryankanno/cookiecutter-pyNote: If you want to use the auto approve / merge Dependabot workflow, make
sure to create tags major, minor, patch so that Dependabot can tag its
PRs. The workflow won't merge anything with a major tag.
cruft create https://github.com/ryankanno/cookiecutter-py/The template includes a comprehensive Justfile with the following commands:
Development:
just install- Install dependencies using uvjust clean- Remove all build, test, and documentation artifacts
Testing & Quality:
just tests- Run all tests (fast, no coverage)just tests -- path/to/test.py- Run specific testsjust tests -- --durations=10- Show slow test durationsjust coverage- Run tests with comprehensive coverage analysisjust lint- Run all linting checks via toxjust lint --fix- Auto-fix linting issues where possiblejust pre-commit- Run pre-commit hooks
Documentation & Distribution:
just docs- Build Sphinx documentationjust dist- Build distribution packages
Utilities:
just tox -- <args>- Run tox with custom argumentsjust watch <recipe>- Watch files and re-run commands on changes (with desktop notifications)
All commands use uv as the package manager and integrate with tox environments for consistency.
To build the container:
DOCKER_BUILDKIT=1 docker build .To run the container (if you've installed the defaults):
docker run <image_id or tag> python -m surf.surfIf you enable the PyPi workflow, versioning will happen via dunamai within the Github pipeline.
If instead, you prefer to version your package, please do it via uv version $(dunamai from any) as recommended in their documentation.
When you run cookiecutter, you'll be prompted for various configuration options. Here are the key variables:
Project Information:
author_name- Your full nameauthor_email- Your email addressproject_name- Human-readable project nameproject_short_description- Brief description of your projectproject_url- Project repository URLproject_license- License type (default: MIT)github_repository_owner- GitHub username or organizationpackage_name- Python package name (snake_case)version- Initial version number (default: 0.0.0)
Python & Dependencies:
python_version- Minimum Python version (default: 3.12)supported_python_versions- Comma-separated list of supported versionsuv_version- Version of uv to usetox_version- Version of tox to usesphinx_theme- Documentation theme (select from multiple options)
Optional Features:
should_use_direnv- Include .envrc for direnv (default: y)should_create_author_files- Include AUTHORS.rst (default: y)should_install_github_dependabot- Enable Dependabot (default: y)should_automerge_autoapprove_github_dependabot- Auto-approve/merge Dependabot PRs (default: y)should_install_github_actions- Include GitHub Actions workflows (default: y)should_upload_coverage_to_codecov- Upload coverage to Codecov (default: y)
Publishing Options:
should_publish_to_testpypi- Publish to TestPyPI on every main push (default: y)should_publish_to_pypi- Publish to PyPI on release (default: y)should_publish_to_github_packages- Publish to GitHub Packages on release (default: n)should_attach_to_github_release- Attach build artifacts to GitHub releases (default: n)
Note: Even if all publishing options are disabled, the build job still runs and uploads artifacts to GitHub Actions for manual inspection.
See the open issues for a list of proposed features (and known issues).
- add mutmut example to template
- add hypothesis example to template
- add licenses
- add typeguard
- version releases
- update docs
- add publish docs workflow
- include cookiecutter var descriptions
- update default/initial template doc structure
- investigate uv
- migrate to uv
- finalize v1.0.0
Contributions are very much appreciated.
- Fork the project
- Create your feature branch (
git checkout -b feature/new-cookiecutter-feature) - Commit your changes (
git commit -m 'Added a new feature') - Push to the feature branch (
git push origin feature/new-cookiecutter-feature) - Open a PR! π
Distributed under the MIT License. See LICENSE for more information.
Ryan Kanno - @ryankanno
Project Link: https://github.com/ryankanno/cookiecutter-py