Chapter 10: Scaling and solidifying your practices
Project template with cookiecutter
cookiecutter is a tool to create templates for projects, including Python projects. The idea is to delegate boilerplate code to the cookiecutter generation tool.
- Everything in the folder where cookiecutter runs is rendered as a Jinja2 template.
- Variable defaults are defined in
cookiecutter.json. - You can even give templated names to folders and files.
- To use with github actions, think of using with
{% raw %} {% endraw %}block. - Here is an example of a cookiecuter template for a python project.
- You can run directly from a git repository containing the cookiecutter template.
Hint
Symlinks are followed and replaced by the files they point to. In order to keep
the symlinks you need to add a post processing script. To do that, create a folder
hooks in the cookiecutter template root directory and then create a python or
shell script called post_gen_project.sh. More information here
Namespace package
- A feature of python that allows splitting up a project in multiple packages and use a common namespace to import.
from geometry import polygon, lines # Namespace package
# instead of
import geometry_polygons
import geometry_lines
- The convention is that a namespace package should contain another python packages
but should not contain a
__init__.py. By following this convention, one can have two packages without a common ancestor folder to be resolved over the same namespace package.
- geometry-polygons/
- src/geometry/polygons
- __init__.py
- geometry-lines/
- src/geometry/geometry
- __init__.py
Private PyPI server
- pypiserver
- GitHub action cannot publish a package in a private PyPI server by default. But it can be configured to do so. This requires some configuration on the server to allow accesses from GitHub actions.
- Artifactory is a multilanguage package index.
Hint
Use base_python variable in tox.ini to specify the python version to use in the build
task, for example.