I would be shocked to find out that this is the best way to do this ... However, since early python2.x versions, monkey patching __import__
has been supported. We can take advantage of that here:
try:
import builtins # python3.x
except ImportError:
import __builtin__ as builtins # python2.x
import sys
import collections
_builtin_import = builtins.__import__
def _my_import(name, globals=None, locals=None, fromlist=(), level=0):
already_imported = name in sys.modules
mod = _builtin_import(
name,
globals=globals,
locals=locals,
fromlist=fromlist,
level=level)
if not already_imported and name in _post_import_hooks:
for hook in _post_import_hooks[name]:
hook()
return mod
builtins.__import__ = _my_import
_post_import_hooks = collections.defaultdict(list)
def on_import(name):
def decorator(func):
_post_import_hooks[name].append(func)
return func
return decorator
@on_import('numpy')
def print_hi():
print('Hello Numpy')
print('before numpy')
import numpy
print('after numpy')
This answer makes a super simple registry for registering callbacks. The decorator just registers the function and then returns it. It doesn't do any fancy checking (for whether the module is already loaded, for example), but could easily be extended to do that.
Obviously the downside is if some other module decides to monkey patch __import__
, then you're out of luck -- Either this module or the other one is likely to end up broken.
I've tested this and it seems to work on both python2.x and python3.x.
on_import
hook in the module you provide, you might as well import numpy and run that function right away (since modules are global across the entire interpreter). The other consideration is that this "feature" is implicit and hidden to users of the module you provide, which they may not desire. – Caroylncarpon_import
hook - that is what I'm asking. And I don't want to always import numpy because it is slow or may be unavailable. – Boholimport numpy as np
in the shell, my function would get called. – Boholimp.when_imported
to match PEP 369. Hopefully that should remove confusion. – BoholImportError
. – Barbera