modulefinder
---查找脚本使用的模块¶
源代码: Lib/modulefinder.py
此模块提供 ModuleFinder
类,可用于确定由脚本导入的模块集。 modulefinder.py
也可以作为脚本运行,将python脚本的文件名作为参数,然后打印导入模块的报告。
- modulefinder.AddPackagePath(pkg_name, path)¶
记录名为的包 pkg_name 可以在指定的 path .
- modulefinder.ReplacePackage(oldname, newname)¶
允许指定名为 老字号 实际上是名为 纽名 .
- class modulefinder.ModuleFinder(path=None, debug=0, excludes=[], replace_paths=[])¶
此类提供
run_script()
和report()
方法确定由脚本导入的模块集。 path 可以是要搜索模块的目录列表;如果未指定,sys.path
使用。 调试 设置调试级别;值越大,类将打印有关它正在执行的操作的调试消息。 排除 是要从分析中排除的模块名称列表。 replace_paths 是一个列表(oldpath, newpath)
将在模块路径中替换的元组。- report()¶
将报告打印到标准输出,列出脚本导入的模块及其路径,以及丢失或似乎丢失的模块。
- run_script(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__
sre_compile: isstring,_sre,_optimize_unicode
_sre:
sre_constants: REPEAT_ONE,makedict,AT_END_LINE
sys:
re: __module__,finditer,_expand
itertools:
__main__: re,itertools,baconhameggs
sre_parse: _PATTERNENDERS,SRE_FLAG_UNICODE
array:
types: __module__,IntType,TypeType
---------------------------------------------------
Modules not imported:
guido.python.ham
baconhameggs