dir(package) not listing out containing modules in python

北城以北 提交于 2019-12-11 03:42:07

问题


I have a directory structure as follows:

TestCases:

  • __init__.py
  • agent_cases.py # contains many classes eg: foo, bar.

TestSuite:

  • __init__.py
  • main.py

In main.py,

import TestCases
dir(TestCases.agent_cases)

I expect to find the list of classes including foo, bar, etc. I get an error as follows:

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

Also dir(TestCases) doesn't return the module names under it. What am I missing? Please help.


回答1:


you either need to import agent_cases in __init__.py or to directly import TestCases.agent_cases in your code.

This is because modules in a directory module is not imported automatically, you need to do it explicitely, or document it so your users import them directly.




回答2:


Sub-modules are not automatically imported when you import the package. You can explicitly import agent_cases in the TestCases.__init__ module:

import agent_cases

if you expect the module to always be imported with the package.



来源:https://stackoverflow.com/questions/23829845/dirpackage-not-listing-out-containing-modules-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!