Example Python project that demonstrates how to create a Python package using the latest
Python testing, linting, and type checking tooling. The project contains a my_module
package (my_module.libs
) and a command line interface (my_module.main
).
Python 3.10+.
This package uses Poetry to manage dependencies and isolated Python virtual environments.
To proceed, install Poetry globally onto your system.
Dependencies are defined in pyproject.toml
and specific versions are locked
into poetry.lock
. This allows for exact reproducible environments across
all machines that use the project, both during development and in production.
To install all dependencies into an isolated virtual environment:
Append
--remove-untracked
to uninstall dependencies that are no longer in use from the virtual environment.
$ poetry install
To activate the virtual environment that is automatically created by Poetry:
$ poetry shell
To deactivate the environment:
(my_module) $ exit
To upgrade all dependencies to their latest versions:
$ poetry update
This project is designed as a Python package, meaning that it can be bundled up and redistributed as a single compressed file.
Packaging is configured by:
To package the project as both a source distribution and a wheel:
$ poetry build
This will generate dist/my_module-1.0.0.tar.gz
and dist/my_module-1.0.0-py3-none-any.whl
.
Read more about the advantages of wheels to understand why generating wheel distributions are important.
Source and wheel redistributable packages can
be published to PyPI or installed
directly from the filesystem using pip
.
$ poetry publish
Note: To enable publishing, remove the
"Private :: Do Not Upload"
trove classifier.
Automated code quality checks are performed using
Nox and
nox-poetry
. Nox will automatically create virtual
environments and run commands based on noxfile.py
for unit testing, PEP 8 style
guide checking, type checking and documentation generation.
Note:
nox
is installed into the virtual environment automatically by thepoetry install
command above. Runpoetry shell
to activate the virtual environment.
To run all default sessions:
(my_module) $ nox
Unit testing is performed with pytest. pytest has become the de facto Python unit testing framework. Some key advantages over the built-in unittest module are:
- Significantly less boilerplate needed for tests.
- PEP 8 compliant names (e.g.
pytest.raises()
instead ofself.assertRaises()
). - Vibrant ecosystem of plugins.
pytest will automatically discover and run tests by recursively searching for folders and .py
files prefixed with test
for any functions prefixed by test
.
The tests
folder is created as a Python package (i.e. there is an __init__.py
file within it)
because this helps pytest
uniquely namespace the test files. Without this, two test files cannot
be named the same, even if they are in different subdirectories.
Code coverage is provided by the pytest-cov plugin.
When running a unit test Nox session (e.g. nox -s test
), an HTML report is generated in
the htmlcov
folder showing each source file and which lines were executed during unit testing.
Open htmlcov/index.html
in a web browser to view the report. Code coverage reports help identify
areas of the project that are currently not tested.
pytest and code coverage are configured in pyproject.toml
.
To pass arguments to pytest
through nox
:
(my_module) $ nox -s test -- -k test_rules
PEP 8 is the universally accepted style guide for
Python code. PEP 8 code compliance is verified using Flake8. Flake8 is
configured in the [tool.flake8]
section of pyproject.toml
. Extra Flake8 plugins are also
included:
flake8-bugbear
: Find likely bugs and design problems in program.flake8-broken-line
: Forbid using backslashes (\
) for line breaks.flake8-comprehensions
: Helps write betterlist
/set
/dict
comprehensions.pep8-naming
: Ensure functions, classes, and variables are named with correct casing.pyproject-flake8
: Allow configuration offlake8
throughpyproject.toml
.
Some code style settings are included in .editorconfig
and will be
configured automatically in editors such as PyCharm.
To lint code, run:
(my_module) $ nox -s lint
Code is automatically formatted using black. Imports are automatically sorted and grouped using isort.
These tools are configured by:
To automatically format code, run:
(my_module) $ nox -s fmt
To verify code has been formatted, such as in a CI job:
(my_module) $ nox -s fmt_check
Type annotations allows developers to include optional static typing information to Python source code. This allows static analyzers such as mypy, PyCharm, or Pyright to check that functions are used with the correct types before runtime.
For PyCharm in particular, the IDE is able to provide much richer auto-completion, refactoring, and type checking while the user types, resulting in increased productivity and correctness.
def create_rules(some_parameter: str | None = None) -> bool: # noqa: D103
return True
...
mypy is configured in pyproject.toml
. To type check code, run:
(my_module) $ nox -s type_check
See also awesome-python-typing.
PEP 561 defines how a Python package should communicate the presence of inline type annotations to static type checkers. mypy's documentation provides further examples on how to do this.
Mypy looks for the existence of a file named py.typed
in the root of the
installed package to indicate that inline type annotations should be checked.
Continuous integration is provided by GitHub Actions. This runs all tests, lints, and type checking for every commit and pull request to the repository.
GitHub Actions is configured in .github/python.yml
.
Material for MkDocs is a powerful static site generator that combines easy-to-write Markdown, with a number of Markdown extensions that increase the power of Markdown. This makes it a great fit for user guides and other technical documentation.
The example MkDocs project included in this project is configured to allow the built documentation to be hosted at any URL or viewed offline from the file system.
To build the user guide, run,
(my_module) $ nox -s docs
and open docs/user_guide/site/index.html
using a web browser.
To build and serve the user guide with automatic rebuilding as you change the contents, run:
(my_module) $ nox -s docs_serve
and open http://127.0.0.1:8000 in a browser.
Each time the master
Git branch is updated, the
.github/pages.yml
GitHub Action will
automatically build the user guide and publish it to GitHub Pages.
This is configured in the docs_github_pages
Nox session. This hosted user guide
can be viewed at https://rms-sth.github.io/python-package-structure/.
This project uses mkdocstrings plugin for MkDocs, which renders Google-style docstrings into an MkDocs project. Google-style docstrings provide a good mix of easy-to-read docstrings in code as well as nicely-rendered output.
"""compute and parse rules for validation
Args:
some_parameter (str | None, optional): parameter for function. Defaults to None.
Returns:
bool: parsed rules.
"""
Traditionally, Python projects place the source for their packages in the root of the project structure, like:
my_module
├── my_module
│ ├── __init__.py
│ ├── main.py
│ └── libs
├── tests
│ ├── __init__.py
│ └── my_module
│ ├── libs
│ └── test_fact.py
├── noxfile.py
└── pyproject.toml
However, this structure
is known to
have bad interactions with pytest
and nox
, two standard tools maintaining Python projects. The
fundamental issue is that Nox creates an isolated virtual environment for testing. By installing
the distribution into the virtual environment, nox
ensures that the tests pass even after the
distribution has been packaged and installed, thereby catching any errors in packaging and
installation scripts, which are common. Having the Python packages in the project root subverts
this isolation for two reasons:
- Calling
python
in the project root (for example,python -m pytest tests/
) causes Python to add the current working directory (the project root) tosys.path
, which Python uses to find modules. Because the source packagemy_module
is in the project root, it shadows themy_module
package installed in the Nox session. - Calling
pytest
directly anywhere that it can find the tests will also add the project root tosys.path
if thetests
folder is a Python package (that is, it contains a__init__.py
file). pytest adds all folders containing packages tosys.path
because it imports the tests like regular Python modules.
In order to properly test the project, the source packages must not be on the Python path. To prevent this, there are three possible solutions:
- Remove the
__init__.py
file fromtests
and runpytest
directly as a Nox session. - Remove the
__init__.py
file from tests and change the working directory ofpython -m pytest
totests
. - Move the source packages to a dedicated
src
folder.
The dedicated src
directory is the
recommended solution
by pytest
when using Nox and the solution this blueprint promotes because it is the least brittle
even though it deviates from the traditional Python project structure. It results is a directory
structure like:
python-package-structure
├── noxfile.py
├── poetry.lock
├── pyproject.toml
├── src
│ └── my_module
│ ├── libs
│ ├── main.py
└── tests
├── __init__.py
└── my_module
└── libs
├── rules.py
└── tasks.py
Licensing for the project is defined in:
This project uses a common permissive license, the MIT license.
You may also want to list the licenses of all the packages that your Python project depends on. To automatically list the licenses for all dependencies in (and their transitive dependencies) using pip-licenses:
(my_module) $ nox -N -s licenses
...
Name Version License
click 8.1.3 BSD License
colorama 0.4.4 BSD License
typer 0.4.1 MIT License
Docker is a tool that allows for software to be packaged into isolated containers. It is not necessary to use Docker in a Python project, but for the purposes of presenting best practice examples, a Docker configuration is provided in this project. The Docker configuration in this repository is optimized for small size and increased security, rather than simplicity.
Docker is configured in:
To build the Docker image:
$ docker build --tag my_module .
To run the image in a container:
$ docker run --rm --interactive --tty my_module
Note: If you need to install Poetry on Alpine Linux, see the pre-built
poetry
package for that platform rather than runningpip install poetry
. This avoids needing to build Poetry dependencies from source.FROM alpine:latest RUN apk add --no-cache poetry
The proper [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix) line for Python scripts is:
#!/usr/bin/env python3
Ubuntu releases come with a default python3
executable. This is frozen for the life of the OS
and only receives security and bug fixes. To install a newer version of Python globally,
consider the deadsnakes PPA.
sudo add-apt-repository ppa:deadsnakes
sudo apt update
sudo apt install python3.10
To integrate automatic code formatters into IDE, reference the following instructions:
-
- The File Watchers method (step 6) is recommended. This will run
black
on every save.
- The File Watchers method (step 6) is recommended. This will run
-
- The File Watchers method (option 1) is recommended. This will run
isort
on every save.
- The File Watchers method (option 1) is recommended. This will run