Overview
Themkinits command scans the project for namespace packages (directories with Python files but no __init__.py) and creates minimal __init__.py files for them. This ensures all packages follow traditional Python package conventions and are properly importable.
What It Does
The command:- Scans the project directory for namespace packages (PEP 420 packages)
- Identifies directories containing
.pyfiles but missing__init__.py - Creates minimal
__init__.pyfiles with docstrings - Excludes the
docs/directory from scanning - Uses parallel execution for performance on large projects
The command is idempotent and non-destructive: safe to run multiple times, only creates missing files, never modifies existing ones.
Usage
Basic Usage
After Adding New Packages
With Verbose Output
Quiet Mode
Expected Output
Generated Files
Created__init__.py files contain a minimal docstring:
Behavior
Automatically finds all directories containing
.py files but missing __init__.py.Creates minimal
__init__.py files with just a docstring. No imports or other content.The
docs/ directory is explicitly excluded from scanning to avoid creating __init__.py in documentation.Never modifies existing
__init__.py files. Only creates new ones.Uses
ThreadPoolExecutor for parallel file creation on large projects.When to Use
Use mkinits When:
- You’ve created new package directories
- Converting from namespace packages (PEP 420) to traditional packages
- Ensuring all packages have
__init__.pyfor better tooling support - Import errors due to missing
__init__.pyfiles - Setting up a new project structure
Example Workflow
Namespace Packages vs Traditional Packages
Namespace Packages (PEP 420)
- Allows split packages across multiple locations
- Some tools have limited support
- Implicit namespace packages
Traditional Packages
- Better tooling support
- Explicit package boundaries
- Standard Python convention
Customizing Generated Content
The default__init__.py is minimal. To customize, you can:
- Run
mkinitsto create the files - Edit the files to add imports,
__all__, or other content - Re-run safely -
mkinitswon’t overwrite existing files
Excluded Directories
The following directories are excluded from scanning:docs/- Documentation directory__pycache__/- Python cache directories.git/- Version control- Hidden directories (starting with
.)
Integration with Other Commands
After init
The init command doesn’t automatically run mkinits. Run it separately if needed:
Before mktests
Run mkinits before generating tests to ensure proper package structure:
With mkroot
Can be run in any order - they’re independent:
Related Commands
- init - Full project initialization
- mktests - Generate test skeletons (benefits from proper package structure)
- mkroot - Create configuration files
Implementation
Themkinits command:
- Calls
find_namespace_packages()to discover namespace packages - Converts package names to directory paths
- Uses
ThreadPoolExecutor.map()to create__init__.pyfiles in parallel - Each file is created via
make_init_module()
pyrig/rig/cli/commands/make_inits.py:14.
Run
uv run pyrig mkinits --help to see the command’s built-in help text.