Skip to main content

Smart Python import wrapper with lazy load and auto-install

Project description

๐Ÿช„ magicimporter

Smart Python import wrapper that makes your imports lazier, safer, and smarter.

Written by a developer who's tired of writing try: import ... except: install ... everywhere.


๐Ÿš€ What It Does

magicimporter is a tiny utility that helps you:

โœ… Lazy-load modules - defer importing until first access
โœ… Auto-install missing packages - no more pip install errors
โœ… Avoid clutter - wrap your imports cleanly and clearly
โœ… Support optional dependencies - without breaking your app

Perfect for:

  • Notebooks and prototyping
  • Reducing startup time (e.g., in CLI tools)
  • Handling optional packages gracefully
  • Plug-and-play with any Python script

๐Ÿ“ฆ Installation

From PyPI (soon):

pip install magicimporter

From source:

git clone https://github.com/Deadpool2000/magicimporter
cd magicimporter
pip install -e .

๐Ÿ”ง How to Use

Basic Usage

from magicimporter import magic_import

json = magic_import("json")  # Just works like import
print(json.dumps({"hello": "world"}))

Lazy Import (delay loading until accessed)

np = magic_import("numpy", lazy=True)

# numpy isnโ€™t imported until this line:
print(np.array([1, 2, 3]))

Auto-Install Missing Packages

requests = magic_import("requests", auto_install=True)

res = requests.get("https://httpbin.org/get")
print(res.status_code)

Lazy + Auto-Install Together

pandas = magic_import("pandas", lazy=True, auto_install=True)

# Nothing is loaded yet...
print("About to create DataFrame")

df = pandas.DataFrame({"a": [1, 2, 3]})  # Now pandas loads
print(df)

Import Submodules

os_path = magic_import("os.path", lazy=True)
print(os_path.basename("/foo/bar.txt"))

Optional Dependencies Pattern

try:
    yaml = magic_import("pyyaml", auto_install=False)
    config = yaml.safe_load(open("config.yaml"))
except ModuleNotFoundError:
    print("Skipping YAML config support: dependency missing")

๐Ÿ“ Directory Structure

magicimporter/
โ”œโ”€โ”€ magicimporter/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ core.py          # main logic
โ”‚   โ”œโ”€โ”€ lazy.py          # lazy loader
โ”‚   โ””โ”€โ”€ installer.py     # auto-installer
โ”œโ”€โ”€ examples/
โ”‚   โ””โ”€โ”€ demo.py
โ”œโ”€โ”€ tests/
โ”‚   โ””โ”€โ”€ test_magicimporter.py
โ”œโ”€โ”€ setup.py
โ”œโ”€โ”€ pyproject.toml
โ””โ”€โ”€ README.md

๐Ÿงช Running Tests

pytest tests/

You can also test individual features manually from examples/demo.py.


๐Ÿง  Why This Exists

You're writing a script or notebook and hit this:

import somepackage  # ModuleNotFoundError

Then you type:

pip install somepackage

And do it again. And again.
What if your code just handled that?
That's what magicimporter does.

Itโ€™s especially useful when:

  • Building CLI tools with optional features
  • Using heavy modules only in specific branches
  • Rapid prototyping in notebooks or scripts

๐Ÿ“Œ Notes & Caveats

  • Auto-install uses subprocess to call pip - no fancy API yet.
  • Lazy import doesn't delay sub-imports inside a module (standard Python behavior).
  • Use responsibly in production: implicit installs may surprise your users.

๐Ÿ› ๏ธ Roadmap Ideas

  • Async import support
  • Config file or env variable overrides
  • Warnings and logging customization
  • Module-level caching

๐Ÿ‘จโ€๐Ÿ’ป Author

Made with care by Deadpool2000 โ€“ feel free to fork, extend, or PR ideas.


๐Ÿ“„ License

MIT โ€“ use it, hack it, ship it.

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

magicimporter-0.1.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

magicimporter-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file magicimporter-0.1.0.tar.gz.

File metadata

  • Download URL: magicimporter-0.1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for magicimporter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cac9375a6a3f05bb25e02e518aabd69d1e67e28ca0c2ac85216c579501b720db
MD5 a435709a3291c9b89f198f5551564cb2
BLAKE2b-256 bf1376c76a0d4d5705044646226b7a88755fe0c679b6376fce606c7eabbfe385

See more details on using hashes here.

File details

Details for the file magicimporter-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: magicimporter-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for magicimporter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 189f7c0a49ad145c728d86c8a9d4f99eab81e6ed80e43971017a4982b310563f
MD5 d29e2011f33e4f9113be567364c703b0
BLAKE2b-256 7eaa54e09a485886abc39ad968320cc985b1d50b0248b332a8f4a237228195c0

See more details on using hashes here.

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