Skip to main content
 首页 » 编程设计

python之dir(package) 没有列出包含 python 中的模块

2024年11月01日10soundcode

我的目录结构如下:

测试用例:

  • __init__.py
  • agent_cases.py # 包含许多类,例如:foo、bar。

测试套件:

  • __init__.py
  • main.py

在main.py,

import TestCases 
dir(TestCases.agent_cases) 

我希望找到包含foo、bar等类的列表,但出现如下错误:

AttributeError: 'module' object has no attribute 'agent_cases' 

此外,dir(TestCases) 不会返回其下的模块名称。 我错过了什么?请帮忙。

请您参考如下方法:

您需要在 __init__.py 中导入 agent_cases 或直接在您的代码中导入 TestCases.agent_cases。

这是因为目录模块中的模块不会自动导入,您需要明确地执行此操作,或者将其记录下来以便您的用户直接导入它们。