The template for a professional Python Package
In October 2022 I wrote this Medium article in which I explain step-by-step on how to create a professional Python Package. To make it extra easy for everyone I’ve also created this repository which can be used as a template. Still, to fully understand all components of this repository I strongly recommend reading the article.
Contents
A src/ folder with example code.
A pyproject.toml with all the necessary dependencies specified.
A tests/ folder with example tests, using pytest and Hypothesis.
Pre-commit hooks for Black, isort and flake8.
A workflow test-package.yaml which automates testing with Github Actions for different Python versions (optionally) on different Operating Systems, using caching to speed up the process.
A docs/ folder with a Sphinx project template for ReadTheDocs which can easily be auto-generated with one command, including automatic API documentation of all code in src/.
Usage
To convert this package template to your own package please take the following steps:
Clone this repository:
git clone https://github.com/sTomerG/my-package.gitCreate a virtual environment using pyenv and pyenv-virtualenv or
python3 -m venv venvActivate the virtual environment using using pyenv or
source venv/bin/activateUpgrade pip and install poetry:
pip install --upgrade pip && pip install poetryActivate the poetry venv with
poetry shellif you’re using pyenv.Change some fields of
[tool.poetry]in pyproject.toml, e.g. the project name, in fieldnameandpackagesChange the name of the my_package/ folder in src/ to the new name of your package (use underscores instead of hyphens here).
Change the top fields in docs/source/conf.py, e.g.
projectto the same value asnamein pyproject.toml.Reinstall the package with
poetry installChange the
my_packagepart infrom my_package.calc importto the name of the folder you chose in step 6 in tests/test_calc/test_square.py and in docs/source/notebooks/usage.ipynb.Check if the tests are still running correctly with working with
poetry run pytestRebuild the docs with
poetry run sphinx-autobuild docs/source docs/build/htmlEdit in the top of the README and docs/source/index.md the links
Note
When you add dependencies with poetry add, make sure to also run poetry export --with doc -f requirements.txt --output docs/rtd_requirements.txt to update the requirements file for the ReadTheDocs.
Contents: