我希望在导入特定模块时运行一些回调。例如(使用一个并不真正存在的假 @imp.when_imported
函数):
@imp.when_imported('numpy')
def set_linewidth(numpy):
import shutil
numpy.set_printoptions(linewidth=shutil.get_terminal_size()[0])
此功能是在 PEP 369: Post import hooks 中设计的但被撤回,原因是:
This PEP has been withdrawn by its author, as much of the detailed design is no longer valid following the migration to importlib in Python 3.3.
但是importlib没有明确的解决方案。如何使用 importlib
实现导入后 Hook ?
最佳答案
我会很震惊地发现这是执行此操作的最佳方法......但是,自早期的 python2.x 版本以来,猴子修补 __import__
一直是支持的。我们可以在这里利用它:
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')
这个答案为注册回调创建了一个 super 简单的注册表。装饰器只是注册函数然后返回它。它不会做任何花哨的检查(例如,模块是否已经加载),但可以很容易地扩展来做到这一点。
显然不利的是,如果其他模块决定对 __import__
进行猴子修补,那么您就不走运了——这个模块或另一个模块都可能最终损坏。
我已经测试过了,它似乎适用于 python2.x 和 python3.x。
关于python - Python 3 中的导入后 Hook ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40623889/