A tool to bundle all local Python imports into a single self-contained file.
import-gobbler takes a Python entry point file and recursively inlines all local imports, producing a single self-contained Python file. It handles:
- Regular imports (
import x) - From imports (
from x import y, z) - Relative imports (
from . import x,from ..x import y) - Nested module structures
- Circular dependencies (via class wrapping)
pip install -e .Or simply use the script directly:
python main.py input_file.py -o output.pyimport-gobbler input_file.py # Output to stdout
import-gobbler input_file.py -o out.py # Output to file
import-gobbler input_file.py -c # Copy to clipboard
import-gobbler input_file.py --root /path/to/projectOr via python directly:
python main.py input_file.py -c- Parses the input Python file using AST
- Identifies all local imports (non-standard-library modules)
- Recursively resolves and inlines each imported module
- Wraps each module as a class to avoid namespace conflicts
- Handles symbol imports and circular dependencies
- Outputs a single, self-contained Python file
Input (main.py):
from helper import greet
def main():
print(greet("World"))Output (bundled):
class _helper:
@staticmethod
def greet(name):
return f'Hello, {name}!'
def main():
print(_helper.greet("World"))pytest test_bundle.py -v- Does not handle C extensions or binary dependencies
- Assumes all imports are resolvable from the project root
- Wrapping modules as classes may have edge cases with complex code