HOW TO CREATE A PYTHON PACKAGE
How To Create A Python Package
Date Published: | |
Last Modified: |
Contents
Overview
PEP 621 introduced the pyproject.toml
method of specifying package metadata, including how to build the package. The idea is that this method supports many different build systems, with the exact build system being mentioned under the [build-system]
table inside the TOML file1.
The three main build systems that are supported as of 2023 are Setuptools, Flit and Poetry1.
Use With setuptools
A basic example of a pyproject.toml
that tells pip to use setuptools
to build the package:
|
|
If you specify the build metadata this way, you DO NOT need to add the setup.py
Python file that setuptools traditionally required.
Note that dependencies are not pulled from a requirements.txt
file, instead you have to list them in the pyproject.toml
file under project.dependencies
. PEP 631 specifies how to write these dependencies (this PEP has been merged into the overarching PEP 621)2.
Once this file is in place, you should be able to run a test installation by pointing pip
at this package on the local file system, e.g.:
|
|
where /path/to/package/
is the path to the directory in where the pyproject.toml
resides.
Optional Dependencies
You can add optional dependencies under a [project.optional-dependencies]
table in the pyproject.toml
file. One common use case is when you have a set of dependencies that are only required to run unit tests, but not when deploying the application to others.
Here is an example which adds three optional “test” dependencies:
|
|
These optional dependencies can be installed (along with the required dependencies) with:
|
|
References
Python (2022, Oct 7). PEP 621 – Storing project metadata in pyproject.toml. Retrieved 2023-05-02, from https://peps.python.org/pep-0621/. ↩︎ ↩︎
Python (2022, Jun 14). PEP 631 – Dependency specification in pyproject.toml based on PEP 508. Retrieved 2023-05-02, from https://peps.python.org/pep-0631/. ↩︎
Authors

This work is licensed under a Creative Commons Attribution 4.0 International License .
Related Content:
- A Comparison Of Serialization Formats
- A Tutorial On geopandas
- Python And File Paths
- rasterio
- September 2019 Updates
Tags
- Python
- packages
- setuptools
- pyproject
- TOML
- build
- build tools
- dependencies
- Setuptools
- Flit
- Poetry
- packaging
- modules