geninit
Keeping __init__.py files up to date gets tedious once a package has more
than a few modules. geninit takes care of generating those files while still letting you
decide what belongs in your public API.
The geninit command itself requires Python 3.15 or newer, but can generate eager
__init__.py files for projects that support older Python versions.
Installation
The simplest way to install geninit is as an isolated uv tool:
uv tool install --python 3.15 geninit
You can then run geninit from any project. If you only want to try it once,
use uvx instead:
uvx --python 3.15 geninit --check
For a project that keeps its development tools in uv.lock, add geninit to the
development dependency group:
uv add --dev geninit
Commands in the rest of this README can then be written as
uv run --locked --no-sync geninit ....
Getting started
Add a [tool.geninit] table to your pyproject.toml and list the packages you
want geninit to manage:
[project]
requires-python = ">=3.14"
[tool.geninit]
roots = ["src/acme"]
Run geninit from the directory containing pyproject.toml:
geninit
The first run creates the necessary __init__.py files. Later runs update the
managed sections when modules or exports change.
You can also pass additional package roots on the command line:
geninit src/acme_plugins
Command-line roots are added to the roots from pyproject.toml.
Choosing what to export
By default, a normal child module is protected: the package exposes the module itself, but does not copy names from that module into the package namespace. Modules whose names start with an underscore are private by default.
Use these declarations in the handwritten part of __init__.py when you want
different behavior:
__public__ = ("api",)
__protected__ = ("models",)
__flattened__ = ("implementation",)
__private__ = ("legacy",)
# <geninit>
# </geninit>
- A public child exposes the module and the names listed in that child's
__all__. - A protected child exposes only the module.
- A flattened child exposes only the names listed in the child's
__all__. - A private child is not exposed by the package.
For example, src/acme/api.py might contain:
__all__ = ("Client", "connect")
class Client: ...
def connect() -> Client:
return Client()
With api declared public, users can then write either
from acme import Client or from acme import api.
Declaring api flattened instead would support the first form without adding
api to the package's generated __all__ or propagating that child name
through public parent packages.
This controls the generated API; importing a child's contents may still cause
Python to bind the child as a runtime package attribute.
The __public__, __protected__, __flattened__, __private__, and child
__all__ declarations must each be assigned once at module scope, using a list
or tuple of literal strings. Names must be unique and resolve to known symbols.
Put the complete list in that assignment: geninit rejects direct mutations such
as append, extend, item assignment, and deletion instead of guessing the
resulting exports.
Managed files
geninit only replaces content between these markers:
# <geninit>
# </geninit>
New and empty __init__.py files are generated automatically. geninit will not
overwrite a nonempty file unless it already contains exactly one matching pair
of markers. To adapt an existing __init__.py file, add the empty marker pair where
you want the generated section to appear, then run geninit.
Place each marker on its own unindented comment line, outside strings and expressions. Marker examples in docstrings do not count. Text outside the managed section is preserved, including its original line endings.
Excluding files and forcing eager imports
exclude removes matching paths from discovery. eager forces selected
modules to use ordinary eager imports:
[tool.geninit]
roots = ["src/acme"]
exclude = ["**/tests/**", "**/fixtures/**"]
eager = ["core.py", "plugins/builtin.py"]
Both options use paths relative to their package root.
geninit reads [project].requires-python to decide which import syntax is
safe. If every supported version is Python 3.15 or newer, imports are lazy
unless they match eager. If the project supports an earlier version, or the
version constraint is missing, all generated imports are eager.
Checking without changing files
Use --check in CI or whenever you only want to verify that the generated
files are current:
geninit --check
Use --diff to print the changes geninit would make:
geninit --diff
The command uses these exit codes:
0means every managed file was already current.1means configuration, input, or generation failed.2means files were changed, or--check/--difffound stale files.
The normal write command deliberately exits with status 2 after changing files. This makes generated changes visible in pre-commit hooks instead of letting a commit continue with unstaged output.
Using geninit with prek or pre-commit
For a uv-managed project, add this local hook to your pre-commit config:
[[repos]]
repo = "local"
[[repos.hooks]]
id = "geninit"
name = "Generate package initializers"
entry = "uv run --locked --no-sync geninit"
language = "system"
pass_filenames = false
always_run = true
stages = ["pre-commit"]
pass_filenames = false is important: geninit works from package roots rather
than a list of staged files. always_run = true makes sure module deletions and
configuration-only changes are noticed too.
If the hook updates anything, review the changes, stage them, and commit again.
If you are using ruff to format and lint your code, be sure to put geninit before ruff in pre-commit. Then, make sure ruff is available in the hook environment so that geninit can use the target project's
Ruff configuration to format and sort the generated section.
CI example
A typical uv-based CI job can verify generated files before running the rest of the checks:
uv sync --locked
uv run --locked --no-sync geninit --check
uv run --locked --no-sync ruff format --check .
uv run --locked --no-sync ruff check .
If an initializer is stale, geninit --check exits with status 2 and the job
fails without modifying the checkout.
Metadata
Release files for geninit 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| geninit-0.3.1.tar.gz | 14.7 kB | Without provenance | Details for geninit-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| geninit-0.3.1-py3-none-any.whl | Python 3 | none | any | Details for geninit-0.3.1-py3-none-any.whl |
Total release size: 32.7 kB
Release files / geninit-0.3.1.tar.gz
| Download URL | geninit-0.3.1.tar.gz |
|---|---|
| Size | 14.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
988f6db3cf04e2da8d84edc0505871cf6782cf812b936a6e7ae38b5a8f3b40e9
|
|
BLAKE2b-256 checksum How to use checksums |
92ebde67a0aef4e76478f5c20ba4ff7453e374278effb522c901936e71680142
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / geninit-0.3.1-py3-none-any.whl
| Download URL | geninit-0.3.1-py3-none-any.whl |
|---|---|
| Size | 18.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e59bb05cc416d6a947b8233337f42ebbdfd2bfa73610b87104c61119c40e3bd1
|
|
BLAKE2b-256 checksum How to use checksums |
637404b9e7bf2811aad9051760ca9eeeeefb9cc5617890284c493705e25b3702
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for geninit 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.