Skip to main content

🪄 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.

Release files for magicimporter 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for magicimporter 0.1.0
File Size Uploaded
magicimporter-0.1.0.tar.gz 5.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for magicimporter 0.1.0
File Interpreter ABI Platform
magicimporter-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.4 kB

Release files / magicimporter-0.1.0.tar.gz

Download URL magicimporter-0.1.0.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
cac9375a6a3f05bb25e02e518aabd69d1e67e28ca0c2ac85216c579501b720db
BLAKE2b-256 checksum
How to use checksums
bf1376c76a0d4d5705044646226b7a88755fe0c679b6376fce606c7eabbfe385
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.5

Release files / magicimporter-0.1.0-py3-none-any.whl

Download URL magicimporter-0.1.0-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
189f7c0a49ad145c728d86c8a9d4f99eab81e6ed80e43971017a4982b310563f
BLAKE2b-256 checksum
How to use checksums
7eaa54e09a485886abc39ad968320cc985b1d50b0248b332a8f4a237228195c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page