I want to install my single file module together with its py.typed file using setuptools and pyproject.toml.
I'm packaging a Python single-module, and so far I was using a very simple pyproject.toml:
[build-system]
requires = ["setuptools==80.9.0"]
build-backend = "setuptools.build_meta"
[project]
name = "sample"
version = 1.0.0
description = "Sample single-module"
Project repository layout is quite simple:
sample
├── pyproject.toml
└── sample.py
The module is installed at the root of site-packages and I can use it like:
from sample import whatever
The problem is, I want to provide a py.typed for this module, so the new repository layout is this:
sample
├── pyproject.toml
├── py.typed
└── sample.py
and the new pyproject.toml reads like this:
[build-system]
requires = ["setuptools==80.9.0"]
build-backend = "setuptools.build_meta"
[project]
name = "sample"
version = 1.0.0
description = "Sample single-module"
[tool.setuptools]
include-package-data = true
package-data = {"sample" = ["py.typed"]}
Of course, setuptools still installs the module at the root of sitepackages and does not install data file py.typed. I was expecting this, and I did not find a clean solution for this, so I switched to a different repository layout, with a package and a module, like this:
sample
├── __init__.py
├── pyproject.toml
└── sample
├── __init__.py
├── py.typed
└── sample.py
This works, but forces me to use the module as import sample.sample or from sample import sample, and I don't want this.
Is there alternative for:
- Having a direct import, no package namespace.
- Having package data installed.
- Avoiding a module subdirectory (not essential).
I know about using __init__.py to import the module, so when I import sample, sample.sample is actually imported, but I'm curious about alternatives.