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.
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.git -
Create a virtual environment using pyenv and pyenv-virtualenv or
python3 -m venv venv -
Activate the virtual environment using using pyenv or
source venv/bin/activate -
Upgrade pip and install poetry:
pip install --upgrade pip && pip install poetry -
Activate 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 fieldnameandpackages -
Change 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 install -
Change 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 pytest -
Rebuild the docs with
poetry run sphinx-autobuild docs/source docs/build/html -
Edit in the top of the README 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.
Metadata
Release files for my-package-tomergabay 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| my-package-tomergabay-0.1.0.tar.gz | 3.8 kB | Without provenance | Details for my-package-tomergabay-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| my_package_tomergabay-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for my_package_tomergabay-0.1.0-py3-none-any.whl |
Total release size: 7.7 kB
Release files / my-package-tomergabay-0.1.0.tar.gz
| Download URL | my-package-tomergabay-0.1.0.tar.gz |
|---|---|
| Size | 3.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0b418dc5a4ba8d3e5153361f9c9bbed2e58056ef018ff294d52091fbab00ed1c
|
|
BLAKE2b-256 checksum How to use checksums |
508b82201a0409b5d04c71557cda680e81992e42d62f78241dfab6830d33c9fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.1 CPython/3.10.3 Darwin/21.6.0
|
Release files / my_package_tomergabay-0.1.0-py3-none-any.whl
| Download URL | my_package_tomergabay-0.1.0-py3-none-any.whl |
|---|---|
| Size | 3.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0e3923ec6faa1030596330859564779748e6ebfeb79b6c42d320fbe09ad1c11a
|
|
BLAKE2b-256 checksum How to use checksums |
a0ddb0a5fac03ab2775f56d955de58715d77e5027368e35ea71a634e62525cd6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.1 CPython/3.10.3 Darwin/21.6.0
|
Security for my-package-tomergabay 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.