The python-package-template repository offers a robust template for creating Python packages. It incorporates best practices for project structure, dependency management, testing, and CI/CD, enabling developers to quickly set up and maintain high-quality Python projects.
- Poetry for Python package management and environment handling.
- Pre-commit hooks to enforce consistent code style, including:
- Pytest for running code tests.
- GitHub Actions for CI/CD, including automated tests, lint checks, and release tagging.
- Install Poetry
- See the Poetry documentation for more details and alternate methods. Examples include:
# using pipx. pipx install poetry
-
Ensure virtual enivornment is intalled in your project directory
poetry config virtualenvs.in-project true` -
Install package dependencies needed for development:
poetry install -
Enable pre-commit hooks in your local environment so they run automatically before every commit:
poetry run pre-commit install -
Update project metadata in:
pyproject.toml:- Change the project name, version, and author information to match your package.
README.md,LICENSE,CHANGELOG.md(optional):- Replace placeholder names, badges, and repository links with those for your project.
For the full Poetry documentation, visit the full docs
-
Adding Dependencies
-
To add a new runtime dependency to your project, use:
poetry add <package_name> -
Example:
poetry add requestsThis updates your
pyproject.tomlunder [project.dependencies] and synchronizes your virtual environment automatically. -
For dev-only dependencies, you can specify --dev:
poetry add pytest --group devThis updates [tool.poetry.group.dev.dependencies] in your pyproject.toml.
-
Poetry provides a way to organize your dependencies by groups. So you can create a new dependency group:
poetry add pytest --group <new-dependency-group>Read more about this here
-
-
Removing Dependencies
- Similarly, to remove a dependency:
Poetry removes the package from your pyproject.toml and uninstalls it from your virtual environment.
poetry remove requests
- Similarly, to remove a dependency:
- Before merging a branch into main to release a new version of your package you will need to update the version number in the pyproject.toml. If you do not update the verrsion number before merging to the main branch the release-and-tag.yml workflow will fail.
Provide a valid bump rule: patch, minor, major, prepatch, preminor, premajor, prerelease.
poetry version <bump-rule>
This project uses GitHub Actions for continuous integration and deployment.
- Linting & Formatting: Runs
pre-commitchecks usingruff. - Testing: Runs
pytestacross Python 3.10, 3.11, and 3.12. - Coverage Upload: Sends test coverage reports to Codecov.
- Tagging & Releasing: Automatically tags a new version based on
pyproject.toml. - Builds the Package: Uses Poetry to create distribution files.
- Creates a GitHub Release: Uploads the built package to GitHub releases.