/pypackage-template

๐Ÿ“‘ A copier template for Python projects managed by Poetry

Primary LanguageJinjaMIT LicenseMIT

Python Package Template

License GitHub Release

This project has been set up to quickly and easily set up a new Python package project instead of copying and modifying files from previous projects every time. For this we use Copier, which will configure your project based on a questionnaire.

Feature Summary

  • โ™ป๏ธ GitHub Actions for continuous integration and publishing to PyPI
  • ๐Ÿ“ฆ Poetry for dependency management and packaging
  • ๐Ÿ›ก๏ธ Thrusted publishers for PyPI releases
  • ๐Ÿณ Dev Containers for easy development in VS Code
  • โฌ†๏ธ Renovate Bot for dependency updates
  • โœ… mypy for static type checking
  • ๐Ÿงช Pytest for testing and code coverage
  • โœ๏ธ Ruff for linting and code formatting
  • And much more!

Create a new project

Ensure you have copier installed via pipx:

pipx install copier

Decide where you want to create your new project, run the following command and answer all the questions:

copier copy --trust gh:klaasnicolaas/pypackage-template ./path/to/destination

Or if you want to use the latest version from the main branch:

copier copy --trust --vcs-ref=HEAD gh:klaasnicolaas/pypackage-template ./path/to/destination

Publishing to PyPI

A GitHub workflow is included that will automatically publish the packaged work to PyPI when a new release is created. To support this, there are some things you need to set up first.

  • Create and verify an account on PyPI.
  • Add a Trusted Publisher to your PyPI account.
  • Create an Environment in the GitHub repository settings and name it "release".

Now, when you create a new release from your repository, a workflow will run and deploy the code to PyPI.

Unit test coverage

With every commit push to a pull request, a GitHub workflow will automatically run unit tests and output code coverage into an xml file. To easily see if code coverage is changing as a result of new work, you should install the GitHub app: Codecov.

  • Go to the Codecov app page - https://github.com/apps/codecov
  • Click Configure
  • Select your repository and follow the instructions
  • Add the CODECOV_TOKEN secret to your repository secrets, you can find the token on the Codecov app page (required since v4.x).

Future pull requests and commits will now include code coverage information.

Renovate Bot

Renovate checks if updates are available for dependencies and will update them automatically via a pull request. If your repository is set up correctly for auto-merging, the pull request will also be merged automatically.

After this Renovate will create a GitHub issue in your repository which serves as a dashboard, you can see here which updates are pending and trigger Renovate to check again.

Automatic Release Drafts

Automatically generate release notes with the Release Drafter workflow. This uses the labels from issues and pull requests to draft pretty and detailed release notes for your GitHub releases.

Updating

Future boilerplate updates can be as simple as:

copier update --trust --skip-answered

In case you want to update your answers to the questions as well as update:

copier update --trust

Start developing on this template

This Python template project relies on Poetry as its dependency manager, providing comprehensive management and control over project dependencies.

You need at least:

Install all packages, including all development requirements:

poetry install

This will also generate a poetry.lock file, you should track this file in version control. Check out the Poetry documentation for more information on the available commands.

Pre-commit hooks

This project uses pre-commit to run some checks before committing code. To install the pre-commit hooks, run the following command in the poetry environment:

pre-commit install

Keep template dependencies up to date

The pyproject.toml file of the template has a jinja2 extension and is therefore not seen by Renovate bot to update automatically. That's why I keep track of the dependencies separately in a Gist and a workflow will check every week if anything needs to be adjusted.

License

Distributed under the MIT License. See LICENSE for more information.