问题
I have a directory structure as follows:
TestCases:
__init__.pyagent_cases.py# contains many classes eg: foo, bar.
TestSuite:
__init__.pymain.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