我已经使用
成功在文件夹中导入了一堆模块from assets import *
现在我想循环浏览这些导入的模块并检查特定的变量或函数。我尝试使用 dir() 函数来获取导入模块的列表并查看它们,但是因为我正在循环遍历字符串数组,而不是技术上的模块数组,所以我无法查找模块 var.
for aModule in dir(assets):
if word in aModule.alt:
print "found it!"
if word in aModule.alt:
AttributeError: 'str' object has no attribute 'alt'
最佳答案
我认为你正在做的事情可以做得更简单:
import assets
for aModule in vars(assets).values():
if hasattr(aModule, 'alt') and word in aModule.alt:
print "found it!"
print aModule.__name__
关于python 循环导入并检查变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5924836/