modulefinder --- 查找脚本使用的模�¶

��: Lib/modulefinder.py


该模��供了一个 ModuleFinder 类,�用于确定脚本导入的模�集。 modulefinder.py 也�以作为脚本�行,给出 Python 脚本的文件�作为�数,之�将打�导入模�的报告。

modulefinder.AddPackagePath(pkg_name, path)¶

记录�为 pkg_name 的包�以在指定的 path 中找到。

modulefinder.ReplacePackage(oldname, newname)¶

�许指定�为 oldname 的模�实际上是�为 newname 的包。

class modulefinder.ModuleFinder(path=None, debug=0, excludes=[], replace_paths=[])¶

该类�供 run_script() 和 report() 方法,用于确定脚本导入的模�集。 path �以是�索模�的目录列表;如果没有指定,则使用 sys.path 。 debug 设置调试级别;更高的值使类打�调试消�,关于它正在�什么。 excludes 是�从分�中排除的模��称列表。 replace_paths 是将在模�路径中替�的 (oldpath, newpath) 元组的列表。

report()¶

将报告打�到标准输出,列出脚本导入的模��其路径,以�缺少或似乎缺失的模�。

run_script(pathname)¶

分� pathname 文件的内容,该文件必须包� Python 代�。

modules¶

一个将模��称映射到模�的字典。 请�阅 ModuleFinder 的示例用法。

ModuleFinder 的示例用法¶

��将分�的脚本(bacon.py):

import re, itertools

try:
    import baconhameggs
except ImportError:
    pass

try:
    import guido.python.ham
except ImportError:
    pass

将输出 bacon.py 报告的脚本:

from modulefinder import ModuleFinder

finder = ModuleFinder()
finder.run_script('bacon.py')

print('Loaded modules:')
for name, mod in finder.modules.items():
    print('%s: ' % name, end='')
    print(','.join(list(mod.globalnames.keys())[:3]))

print('-'*50)
print('Modules not imported:')
print('\n'.join(finder.badmodules.keys()))

输出样例(�能因架构而异):

Loaded modules:
_types:
copyreg:  _inverted_registry,_slotnames,__all__
re._compiler:  isstring,_sre,_optimize_unicode
_sre:
re._constants:  REPEAT_ONE,makedict,AT_END_LINE
sys:
re:  __module__,finditer,_expand
itertools:
__main__:  re,itertools,baconhameggs
re._parser:  _PATTERNENDERS,SRE_FLAG_UNICODE
array:
types:  __module__,IntType,TypeType
---------------------------------------------------
Modules not imported:
guido.python.ham
baconhameggs