Skip to content

Tries to bundle all local imports into a single python file

Notifications You must be signed in to change notification settings

SebbeBroman/import-gobbler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ImportGobbler

A tool to bundle all local Python imports into a single self-contained file.

What is this?

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)

Installation

pip install -e .

Or simply use the script directly:

python main.py input_file.py -o output.py

Usage

import-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/project

Or via python directly:

python main.py input_file.py -c

How it works

  1. Parses the input Python file using AST
  2. Identifies all local imports (non-standard-library modules)
  3. Recursively resolves and inlines each imported module
  4. Wraps each module as a class to avoid namespace conflicts
  5. Handles symbol imports and circular dependencies
  6. Outputs a single, self-contained Python file

Example

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"))

Running Tests

pytest test_bundle.py -v

Limitations

  • 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

About

Tries to bundle all local imports into a single python file

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages