首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

modulefinder

2.3版本的新功能。

该模块提供了一个ModuleFinder可用于确定由脚本导入的模块集的类。modulefinder.py也可以作为脚本运行,以Python脚本的文件名作为参数,之后将打印导入模块的报告。

modulefinder.AddPackagePath(pkg_name, path)

记录名称为pkg_name的包可以在指定的路径中找到。

modulefinder.ReplacePackage(oldname, newname)

允许指定名为oldname的模块实际上是名为newname的软件包。最常见的用法是处理_xmlplus软件包如何替换xml软件包。

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

该类提供run_script()report()方法,以确定该组由脚本导入模块。路径可以是搜索模块的目录列表; 如果未指定,sys.path则使用。调试设置调试级别; 更高的值使得该类打印关于它在做什么的调试消息。不包括是要从分析中排除的模块名称的列表。replace_paths(oldpath, newpath)将在模块路径中替换的元组列表。

report()

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

run_script(pathname)

分析必须包含Python代码的路径名文件的内容。

modules

字典映射模块名称到模块。请参阅ModuleFinder的示例用法。

1. ModuleFinder的示例用法

稍后将分析的脚本(bacon.py):

代码语言:javascript
复制
import re, itertools

try:
    import baconhameggs
except ImportError:
    pass

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

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

代码语言:javascript
复制
from modulefinder import ModuleFinder

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

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

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

示例输出(可能因架构而异):

代码语言:javascript
复制
Loaded modules:
_types:
copy_reg:  _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:  __getslice__,_PATTERNENDERS,SRE_FLAG_UNICODE
array:
types:  __module__,IntType,TypeType
---------------------------------------------------
Modules not imported:
guido.python.ham
baconhameggs

扫码关注腾讯云开发者

领取腾讯云代金券