查詢 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