Skip to main content

Auto-generate missing __init__.py files to fix Python import issues and ensure proper module discovery.

Project description

pyinitgen logo

PyPI version Python Wheel Release

Build status Codecov Test Coverage Code style: black Ruff Security

Downloads OS Python Versions

License: MIT


pyinitgen

Automated init.py generator for Python packages Ensures every directory in your project is a proper Python package — no more mysterious ModuleNotFoundError surprises.

Perfect for:

Large refactors

Monorepos / multi-package architectures

Auto-generated project structures

Migration from namespace-less directories

CI environments ensuring package integrity


🚀 Features

Feature Description
📂 Recursive Scan Walks the directory tree intelligently to find all Python modules.
🛠️ Auto-creates __init__.py Creates __init__.py files only where they are missing.
🧠 Smart Exclusions Ignores common system and runtime directories by default.
📝 Customized Ignores Supports a .pyinitgenignore file to add your own exclusion rules.
✍️ Custom Content Lets you write custom content to newly created __init__.py files.
👀 Dry-Run Mode Preview which __init__.py files will be created without writing them.
Check Flag Exit with error if files are missing (great for CI).
⚙️ Config File Configure via pyproject.toml or .pyinitgen.toml.
🎯 Project-safe Avoids touching non-Python folders and respects your project structure.
Emoji Status Provides an optional, fancy terminal UX with emoji status indicators.
🔒 Zero Destructive Actions Never overwrites existing files or content.

📦 Installation

pip install pyinitgen


⚙️ Configuration & Advanced Usage

CLI Arguments

Argument Short Description Default
--base-dir <path> Base directory to scan. .
--dry-run Preview changes without writing to disk. false
--quiet -q Suppress all non-error output. false
--verbose -v Show all scanned directories. false
--no-emoji Disable emoji in the final output. false
--init-content "..." Custom content to write to new __init__.py files. ""
--check Check for missing __init__.py files without creating them. Exits with 1 if missing. false
--version Show the program's version number and exit.

Customizing Exclusions with Configuration Files

You can configure exclusions in pyproject.toml or .pyinitgen.toml.

Example pyproject.toml:

[tool.pyinitgen]
exclude_dirs = ["legacy", "assets"]

Example .pyinitgen.toml:

[tool.pyinitgen]
exclude_dirs = ["legacy", "assets"]

Customizing Exclusions with .pyinitgenignore

To exclude specific directories from being scanned, create a .pyinitgenignore file in your project's root directory. Each line in this file is treated as a pattern to be excluded.

Note: This feature is ideal for excluding auto-generated folders, data directories, or any other project-specific directories that should not be treated as Python packages.

Example .pyinitgenignore:

# .pyinitgenignore
# Exclude the entire 'assets' directory
assets

# Exclude any directories named 'legacy'
legacy

📝 Example Output

Scanning: src/utils Created src/utils/init.py ✅ Operation complete. Scanned 43 dirs, created 8 new init.py files.


🧩 Why this tool?

Problem Solution

Large Python codebases without -inits Auto insert all required files ModuleNotFoundError during import Ensures folders become packages Hand-creating 50+ init.py files One command 🤖 Accidental file writes? Only creates missing files


⚙️ CLI Help

pyinitgen --help


🛡️ Safe by Design

Never touches existing files

Ignores system & irrelevant dirs by default

Supports dry-run to preview


💡 Tip

Use in CI to guarantee package consistency:

pyinitgen --dry-run


🏗️ Architecture

The project is structured as a standard Python CLI application:

src/
└── pyinitgen/
    ├── __init__.py
    ├── banner.py   # Renders the ASCII logo
    └── cli.py      # Core logic and CLI argument parsing

The core logic resides in cli.py, which performs the directory scan and __init__.py file creation. The banner.py module is a purely cosmetic addition to improve the user experience.


🗺️ Roadmap

  • Add support for customizing the default exclusion list via a configuration file.
  • Add a --check flag that will exit with a non-zero status code if any __init__.py files are missing, but will not create them.
  • Implement a --watch mode to automatically create __init__.py files as new directories are created.

🤝 Contributing

PRs welcome — improve detection logic, add custom exclusion rules, enhance output UX.

👉 Repo: https://github.com/dhruv13x/pyinitgen


📜 License

MIT


🧭 Related Tools in the Suite

Tool Purpose

importdoc Import issue diagnosis import-surgeon Safe import refactoring pypurge Clean caches, venv junk pyinitgen Generate missing init.py ✅ (this project)


⭐ Support

If you like this tool:

⭐ Star the GitHub repo

🐍 Use it in CI & projects

📦 Recommend to Python dev friends


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyinitgen-4.0.0.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyinitgen-4.0.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file pyinitgen-4.0.0.tar.gz.

File metadata

  • Download URL: pyinitgen-4.0.0.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyinitgen-4.0.0.tar.gz
Algorithm Hash digest
SHA256 0bac6c3160ffb2b2c7da1eb4877bdd9b668f772d9985fb620b61f56fd23fc445
MD5 3d59261a1c55d08ce6482135acccc9ac
BLAKE2b-256 2178551a92422ed9d16efe59402349b91c2824cbe2046354f7da1d8d5db3cf8f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyinitgen-4.0.0.tar.gz:

Publisher: publish.yml on dhruv13x/pyinitgen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyinitgen-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyinitgen-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyinitgen-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 25a0ad981ab7660c6533a6e2116e582635c2ea8bcfd02fe4b500f2d1b4c7625f
MD5 d39cd1d8f2b7a4ce0fd6f341b9e94ffd
BLAKE2b-256 bacd4ae67e054f8e98035a3c3da41fbef67e8b90332a3a6d6c03cab771df3317

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyinitgen-4.0.0-py3-none-any.whl:

Publisher: publish.yml on dhruv13x/pyinitgen

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page