mypantry
Lightweight syntactic sugar (~300 lines, zero dependencies) for optional Python packages.
mypantry wraps importlib.metadata to check package availability, import modules
safely, and guard functions with decorators. Works everywhere — development,
installed packages, Docker, notebooks, REPLs. No configuration files needed.
The entire source is one file:
_registry.py.
At a Glance
import pantry
# Guard imports at the top of the file
if pantry.has("numpy"):
import numpy as np
if pantry.has("pillow"):
from PIL import Image
# Guard functions with the decorator
@pantry("numpy", "pillow")
def process(path):
img = Image.open(path)
return np.array(img)
# Or use pantry directly
PIL = pantry["pillow"] # raise if missing
np = pantry.get("numpy") # None if missing
pantry.version("numpy") # "1.26.4"
print(pantry.report("numpy", "pillow", "redis"))
Installation
pip install mypantry
Zero dependencies. Only Python standard library.