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.
🎯 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. ""
--version Show the program's version number and exit.

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.
  • Implement a --watch mode to automatically create __init__.py files as new directories are created.
  • 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.

🤝 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-3.0.2.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

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

pyinitgen-3.0.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyinitgen-3.0.2.tar.gz
Algorithm Hash digest
SHA256 0721b079e7283a0a2da15e8225fa1252d6b635eae0c4e4fed7d0bd222ab80ec8
MD5 8fc9ba5ea19662b283ec00806b603815
BLAKE2b-256 ec296ee3a981e0dc826fa178694bbf827b18083cd1cc38c72b3757dc57efd75f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyinitgen-3.0.2.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-3.0.2-py3-none-any.whl.

File metadata

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

File hashes

Hashes for pyinitgen-3.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 aecd7f836151fc9cff0658a1b8c9b2334405ca067a210328829b05cf934a5932
MD5 89c20360365a969fd7c6d9ce991b61aa
BLAKE2b-256 cc898aaa91b471273bb0ea6f0ae751eb71e62f5eb1633af6dadc3e95cf38fdbf

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyinitgen-3.0.2-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