【发布时间】:2011-08-20 22:57:51
【问题描述】:
我已经使用
成功地在一个文件夹中导入了一堆模块from assets import *
现在我想遍历那些导入的模块并检查特定的变量或函数。我尝试使用 dir() 函数来获取导入模块的列表并查看它们,但是因为我正在循环遍历字符串数组,而不是技术上的模块数组,所以我无法查找模块 var。
for aModule in dir(assets):
if word in aModule.alt:
print "found it!"
如果 aModule.alt 中的单词:
AttributeError: 'str' 对象没有属性 'alt'
【问题讨论】:
-
我在这里找到了解决方案loquehumaine.wordpress.com/2011/04/05/…。必须使用 sys.modules["assets."+item].alt
标签: python import for-loop dir