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
subprocessto 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cac9375a6a3f05bb25e02e518aabd69d1e67e28ca0c2ac85216c579501b720db
|
|
| MD5 |
a435709a3291c9b89f198f5551564cb2
|
|
| BLAKE2b-256 |
bf1376c76a0d4d5705044646226b7a88755fe0c679b6376fce606c7eabbfe385
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
189f7c0a49ad145c728d86c8a9d4f99eab81e6ed80e43971017a4982b310563f
|
|
| MD5 |
d29e2011f33e4f9113be567364c703b0
|
|
| BLAKE2b-256 |
7eaa54e09a485886abc39ad968320cc985b1d50b0248b332a8f4a237228195c0
|