A template to quickly get you creating an open-source python library or project with linting, static analysis, CI, and CD to PyPI.
To use this template, click the green "Use this template" button in the github web interface. Then run:
git clone YOUR_REPO
# then cd into your local repo, and run:
./bootstrap
And follow the on-screen prompts. bootstrap
uses some git data (like detecting your username and repository name), so cloning the repo generated from the template is necessary.
General sanity checks and best practices are performed on provided responses. To disable these, run with the --no-verify
flag:
./bootstrap --no-verify
This template's bootstrap
functionality only works on MacOS/Linux/WSL, it will not work natively on windows.
The resulting project, however, may be windows-compatible.
Features dependent if project is a library or a standalone project.
Poetry support.
- If not installed, Poetry will automatically be installed when running
bootstrap
. - Poetry Dynamic Versioning - Dynamically handles your project version based on git tags.
- If not installed, Poetry will automatically be installed when running
Optional command line interface boilerplate using Cyclopts.
Optional C binding support via Cython.
-
To setup, goto ReadTheDocs Dashboard and click on "Import a Project".
To locally build the docs:
cd docs/ make html
This results in html files in
docs/build/html/
. Double clickdocs/build/html/index.html
to view the docs in your web browser.
Pre-commit linting and static analysis. The following hooks are pre-configured and will automatically run on
git commit
:Docker support for standalone projects.
GitHub Actions for:
- Running
pre-commit
on pull requests and commits tomain
. - Running unit tests, coverage, and verify docs build on pull requests and commits to
main
.- Goto your Codecov Dashboard and add your repo.
- Build and upload wheels to PyPI on semver tags
vX.Y.Z
.- Add your PyPI API token to your GitHub secrets for key
PYPI_TOKEN
. - If using Cython, pre-built binary packages will be created for all major operating systems, python versions, and computer architectures.
- Add your PyPI API token to your GitHub secrets for key
- Build and upload docker images to Dockerhub.
- Add your Dockerhub username and token to your GitHub secrets
DOCKERHUB_USERNAME
andDOCKERHUB_TOKEN
. - Optionally, modify the
tags
field in.github/workflows/docker.yaml
. By default, it assumes your docker username is the same as your github username.
- Add your Dockerhub username and token to your GitHub secrets
- Running
This template has an option to add boilerplate for Cython.
Cython is a programming language that simplifies the creation of C extensions for Python.
The Cython documentation is quite good; the aim of this section is to explain what this
template sets up, and what actions will still need to be performed by you.
This explanation assumes you are familiar with C.
Replace any reference here to pythontemplate
with your project name.
- Place all C and header files in the
pythontemplate/_c_src
directory. If you don't plan on using any explicit C files, you may delete this directory. - Update
pythontemplate/cpythontemplate.pxd
with header information from the files in (1). Example of common definitions (functions, structs, and enums) are provided. Think of*.pxd
as a header file that allows Cython.pyx
code to access pure C.c
files. This file will be compiled into a package of the same name that can be imported in a.pyx
file viacimport
. If you don't plan on using any explicit C files, you may delete this file and the_c_src
directory. - Add Cython code to
pythontemplate/_c_extension.pyx
. Some class starter code is provided. This is where a good pythonic interface (functions and classes) should be written. - If adding type hints, update
pythontemplate/_c_extension.pyi
to reflect your.pyx
implementation. - Optionally tweak
build.py
(runs at setup/installation) with compiler options. The defaultbuild.py
offers a good, working starting point for most projects and performs the following:- Recursively searches for all C files in
pythontemplate/_c_src/
. To change this action, modify the variablec_files
. - Compiles the code defined in
_c_extension.pyx
into a shared object file. - Adds
pythontemplate
andpythontemplate/_c_src
to the Include Path (python variableinclude_dirs
). - If your codebase contains a slower, python implementation of your Cython code,
we can allow building to fail by uncommenting the
allowed_to_fail
logic at the top. The logic checks for the environment variableCIBUILDWHEEL
because we don't want to allow build failures in our CI when creating pre-built wheels that we upload to PyPI.
- Recursively searches for all C files in
- The Github Action workflow defined in
.github/workflows/build_wheels.yaml
will create pre-built binaries for all major Python versions, operating systems, and computer architectures. It will also create a Source Distribution (sdist). All of these distributions will be uploaded to the github action job page. On git semver tags (vX.X.X
), they will be uploaded to PyPI.
When developing, you must re-run poetry-install
to re-compile changes made in C/Cython code.
The resulting, built Cython code will be importable from pythontemplate._c_extension
, so it may be
good to add something like the following to your pythontemplate/__init__.py
:
__all__ = [
"Foo",
]
from pythontemplate._c_extension import Foo
If you find this in the git history of a project and you like the structure, visit this template at https://github.com/BrianPugh/python-template .