查找 Python 脚本使用的模块(ModuleFinder)
'modulefinder' 模块中的 ModuleFinder 类可以确定某个脚本导入的模块集。该模块具有命令行界面和编程界面。
如需演示此功能,请使用以下脚本
#modfinder.py import hello try: import trianglebrowser import nomodule,mymodule except ImportError: pass
命令行界面
以下命令显示已找到和未找到的模块列表。
E:\python37>python -m modulefinder modfinder.py
输出
Name File ---- ---- m __main__ modfinder.py m hello hello.py m math m trianglebrowser trianglebrowser.py Missing modules: ? mymodule imported from __main__ ? nomodule imported from __main__
编程界面
此模块中的ModuleFinder 类提供 run_script() 和 report() 方法来确定脚本导入的模块集。
report()
此方法将一份报告打印到标准输出,其中列出了脚本导入的模块及其路径,以及缺少或看似缺少的模块。
run_script()
此方法分析给定文件的文本内容,文件必须包含 Python 代码。
modules
这是一个将模块名称映射到模块的词典。
badmodules
这是一个无法加载的模块列表。
示例
import modulefinder
modfind=modulefinder.ModuleFinder()
modfind.run_script('modfinder.py')
print ('Modules loaded:')
for k,v in modfind.modules.items():
print (k,v)
print ('not found:')
for i in modfind.badmodules.keys():
print (i)输出
Modules loaded:
__main__ Module('__main__', 'modfinder.py')
hello Module('hello', 'E:/python37\hello.py')
trianglebrowser Module('trianglebrowser', 'E:/python37\trianglebrowser.py')
math Module('math')
not found:
nomodule
mymodule
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP