2

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Kevin
  • 566
  • 2
  • 5
  • 13

2 Answers2

2

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.

zmo
  • 24,463
  • 4
  • 54
  • 90
  • I have imported TestCases.agent_cases in main.py. Is that not sufficient? – Kevin May 23 '14 at 12:58
  • yes, but `agent_cases` will still not appear as member of `TestCases`, it's just an import path. – zmo May 23 '14 at 12:59
0

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks. I needed to "import TestCases.agent_cases". This worked. I didn't include in the init file as I wouldn't require it everytime. Thanks for the direction anyway. – Kevin May 23 '14 at 13:00