[英]get names of all dynamic and second level imports
我在一個包含許多子目錄/包的復雜項目上工作。
有沒有辦法打印給定項目目錄中使用的所有二級+級別/動態模塊?
例如,如果我使用以下導入: from core.dependency.checkpoints_dependency.checkpoints_manager import CheckpointsManager
或import scipy
,我希望打印core.dependency.checkpoints_dependency.checkpoints_manager
和scipy
。
嘗試使用 os.walk 解析目錄,然后檢查系統模塊中的目錄:
import sys
import os
all_modules = []
for dirpath, dirnames, filenames in os.walk(YOUR_PROJECT_DIRECTORY):
module_names = set(sys.modules)
modules_in_file = [sys.modules[name] for name in module_names]
all_modules.extend(modules_in_file)
print(set(all_modules))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.