Mkinit is a tool that automatically generates or updates __init__.py files for Python packages, making imports cleaner and more maintainable.
Mkinit scans Python modules in a package and automatically generates appropriate import statements for the __init__.py file. This helps:
- Maintain consistent and up-to-date package exports
- Reduce manual work when adding new modules to a package
- Ensure proper package structure and imports
- Simplify import statements for users of your package
Mkinit is included as a development dependency:
# Install with other development dependencies
uv sync --devTo install it directly:
uv pip install mkinitIn this project, Mkinit is used to:
- Automatically generate and update
__init__.pyfiles - Ensure consistent package exports
- Simplify the package structure for users
- Run as part of the development workflow
Mkinit is configured as a poethepoet task:
[tool.poe.tasks]
mkinit = "mkinit"To run Mkinit on the project:
# Run via poethepoet
uv run poe mkinit
# Run directly on a specific package
uv run mkinit src/your_package# Generate __init__.py with explicit imports
uv run mkinit --nomodo src/your_package
# Generate __init__.py with __all__ statements
uv run mkinit --all src/your_package
# Specify a custom pattern for modules to include
uv run mkinit --pattern="*.py" src/your_package
# Dry run (show what would be generated without writing files)
uv run mkinit --dry src/your_packageFor a package structure like:
src/your_package/
├── __init__.py
├── module1.py
├── module2.py
└── subpackage/
├── __init__.py
└── module3.py
Mkinit might generate an __init__.py like:
# -*- coding: utf-8 -*-
"""
Package: your_package
"""
from __future__ import absolute_import, division, print_function, unicode_literals
from your_package import subpackage
from your_package.module1 import Function1, Class1
from your_package.module2 import Function2, Class2
__all__ = ["subpackage", "Function1", "Class1", "Function2", "Class2"]You can also use Mkinit with static imports:
# -*- coding: utf-8 -*-
"""
Package: your_package
"""
from __future__ import absolute_import, division, print_function, unicode_literals
# <AUTOGEN_INIT>
from your_package import subpackage
from your_package.module1 import Function1, Class1
from your_package.module2 import Function2, Class2
# </AUTOGEN_INIT>- Run Mkinit after adding new modules: Update your
__init__.pyfiles whenever you add new modules or classes. - Use with version control: Always commit the generated
__init__.pyfiles to your repository. - Consider explicit imports: For better IDE support, use explicit imports rather than wildcard imports.
- Add custom imports: You can add custom imports outside the autogenerated section.
- Document your package structure: Use the generated
__init__.pyfiles as a reference for your package structure.