这是Python的demo show导入钩子。 (http://docs.python.org/2/library/imputil.html) 但是,我这样跟着它:
import sys, __builtin__
original_import = None
# Replacement for __import__()
def import_hook(name, globals=None, locals=None, fromlist=None):
return original_import(name, globals, locals, fromlist)
# Save the original hooks
original_import = __builtin__.__import__
original_reload = __builtin__.reload
# Now install our hooks
__builtin__.__import__ = import_hook
import os
import sys
import win32api
因此,当import win32api
时,import_hook
会被调用,但是,我不确定为什么它对win32api
不起作用,错误消息显示:
TypeError: import_hook() takes at most 4 arguments (5 given)
但是如果我调用import sys
或import os
,它就可以了。
我的Python是2.7.2,操作系统是Windows 7.