The Bare Minimum#
Before we add a bunch of tooling that makes our package easier to maintain over time, it's informative to see the absolute minimum needed to create a Python package.
Project Structure#
For a basic modern Python package, you need:
- A directory containing a
pyproject.toml
file (much more on this soon) - Some code: a Python module or package
That's it!
Really, everything beyond this is optional, and there to make your life easier as a maintainer.
The pyproject.toml
contains the metadata for the package, and instructions for how to build it:
[build-system]
requires = ["hatchling"] # (1)!
build-backend = "hatchling.build"
[project]
name = "my-package"
version = "0.0.1"
-
There are a few different build systems that can be used to build Python packages. We use
hatchling
here, and discuss other options later. Forsetuptools
, this section would look like:
The python module/package contains your code:
Technically, even the python file is optional, but then why are we here?
Installing#
With that, you already have a Python package that can be installed locally with pip
:
# run from inside the my-package directory
pip install .
# run from anywhere
pip install /path/to/my-package
Publishing#
Deploying to PyPI (so that others can pip install
your package) is just a couple lines away:
# run from inside the my-package directory
pip install build twine
python -m build # (1)!
twine upload dist/* # (2)!
build
is a modern Python build frontend that creates a wheel and/or source distribution. (If you are used to runningpython setup.py sdist bdist_wheel
, this replaces that step)twine
is a tool that uploads packages to PyPI.
What about conda?
We'll cover publishing to and installing from conda later.
Next Steps#
While this is sufficient for creating an installable Python package and publishing it to PyPI, it leaves a lot to be desired in terms of maintainability (how do we know that the code works? how do we track changes?). In the next sections, we'll fill in some additional metadata and add some tooling to make our lives easier.