Clone this repository and follow the README.md to start developing your own python package.
- Template package
We use advices, from this blog post.
In summary, the idea is:
- to use
condaas environment manager (allow to install different python version),- conda
environment.ymlis only used to create the "base" environment with python. Poetry is used to install all dependencies.
- conda
poetryas dependency manager:- easy way to have dependencies and
devdependencies, - multiple tools such as formatter, linting, etc. in the
pyproject.toml pyproject.tomlwith everything to build the package- easy to use,
- easy to deploy the package on (private) PyPI.
- easy way to have dependencies and
Install conda:
- on Mac with ARM (M1) processor, install
Miniforgefollow this doc. - on other system you can choose (lighter) Miniconda or (heavier) even Anaconda.
conda env create --file environment.ymlNotes:
- you can rename environment name in
environment.yml(default:package), - by default, it will install
python 3.10.
Activate the environment:
conda activate packageInstall poetry, follow this doc.
Note: do not forget to run source $HOME/.poetry/env to have poetry in your PATH.
Check it works properly:
poetry --versionpoetry installDefault package provided:
pytest: to run tests,pytest-cov: to get coverage withpytest,mypy: static typing checker,black: python formatter.
poetry add pandaspoetry installHere, for instance we implement a greet method to greet someone. Once you installed the package locally you can greet someone:
greet ThomasIt will output Hello Thomas!.
It works thanks to this part:
[tool.poetry.scripts]
greet = "package.main:main"in pyproject.toml. It tell to run the run method in package.main.
We use mypy as static type checker.
mypy .We use black to format python code.
black .pytest --cov=package tests/Versions are managed with git tag based on this blog post. Version in pyproject.toml is just a placeholder.
To get the right based on git tag version in pyproject.toml:
poetry version $(git describe --tags --abbrev=0)It allows to automate release based on git tags.
Note: this is for example only. It should be automated by CI pipeline.
On PyPI, simply:
poetry publishOn a private registry:
poetry config repositories.private http://myprivaterepository:8081/python/
poetry publish -r privateThe CI pipeline is implemented in .github/workflows/ci-cd.yml.
It runs following steps:
- Get version based on git tags,
- Install poetry,
- Run black (formatter) checks,
- Static typing checker (mypy),
- Unit tests,
- Packaging (poetry build),
- Artifacts (coverage, wheel and
tar.gz) upload.
- Merge
developonmain,
git checkout main
git merge develop- Tag
mainwith the version you want,
git tag -a 1.2.3 -m "New release for v1.2.3"- Push
mainandtag,
git push origin main --follow-tags- Rebase
mainondevelop,
git checkout develop
git rebase main- Tag
developwith the next "dev" version.
git tag -a 2.0.0.dev -m "Next version v2.0.0"Do not forget .dev.
Note: we do not exactly follow PEP440. It allows to have a simple release process with tagged release and tagged next version.
- Push
developandtag,
git push origin develop --follow-tags 1.2.3.dev 2.0.0.dev
develop -----|---------\----------|----------->
/ *merge* \ / *rebase*
main ---/-------------|------/------------->
1.2.3
We provide a file .vscode/settings.json to setup pytest in vscode. It allows to run tests directly in vscode with the testing module.