modulefinder
— Find modules used by a script¶
Source code: Lib/modulefinder.py
This module provides a 此模块提供了ModuleFinder
class that can be used to determine the set of modules imported by a script. ModuleFinder
类,可用于确定脚本导入的模块集。modulefinder.py
can also be run as a script, giving the filename of a Python script as its argument, after which a report of the imported modules will be printed.也可以作为脚本运行,将Python脚本的文件名作为其参数,之后将打印导入模块的报告。
-
modulefinder.
AddPackagePath
(pkg_name, path)¶ Record that the package named pkg_name can be found in the specified path.记录在指定path中可以找到名为pkg_name的包。
-
modulefinder.
ReplacePackage
(oldname, newname)¶ Allows specifying that the module named oldname is in fact the package named newname.允许指定名为oldname的模块实际上是名为newname的包。
-
class
modulefinder.
ModuleFinder
(path=None, debug=0, excludes=[], replace_paths=[])¶ This class provides此类提供run_script()
andreport()
methods to determine the set of modules imported by a script.run_script()
和report()
方法来确定脚本导入的模块集。path can be a list of directories to search for modules; if not specified,path可以是用于搜索模块的目录列表;如果未指定,则使用sys.path
is used.sys.path
。debugsets the debugging level; higher values make the class print debugging messages about what it’s doing.设置调试级别;值越高,类将打印有关其正在执行的操作的调试消息。excludesis a list of module names to exclude from the analysis.是要从分析中排除的模块名称列表。replace_paths is a list ofreplace_path是将在模块路径中替换的(oldpath, newpath)
tuples that will be replaced in module paths.(oldpath, newpath)
元组的列表。-
report
()¶ Print a report to standard output that lists the modules imported by the script and their paths, as well as modules that are missing or seem to be missing.将报告打印到标准输出,列出脚本导入的模块及其路径,以及缺失或似乎缺失的模块。
-
run_script
(pathname)¶ Analyze the contents of the pathname file, which must contain Python code.分析pathname文件的内容,该文件必须包含Python代码。
-
modules
¶ A dictionary mapping module names to modules.将模块名称映射到模块的字典。See Example usage of ModuleFinder.请参阅ModuleFinder的用法示例。
-
Example usage of ModuleFinder
ModuleFinder
的用法示例¶
ModuleFinder
The script that is going to get analyzed later on (bacon.py):稍后将分析的脚本(baconpy):
import re, itertools
try:
import baconhameggs
except ImportError:
pass
try:
import guido.python.ham
except ImportError:
pass
The script that will output the report of bacon.py:将输出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()))
Sample output (may vary depending on the architecture):样本输出(可能因架构而异):
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