6

I'm trying to learn how the __init__.py file works for packaging and calling modules from different directories.

I have a directory structure like this:

init_test\
__init__.py
     a\
        aaa.py
     b\
        bbb.py

in aaa.py there is a function called test

bbb.py looks like this:

import init_test.a.aaa
if __name__ == "__main__":
    init_test.a.aaa.test()

but this gives me ImportError: No module named a.aaa

What am I doing wrong? I've tried doing the same basic thing from a module above the package structure as opposed to inside the package and that did not work either? My __init__.py

1

3 Answers 3

7

You also need to have __init__.py in a and b directories

For your example to work first you should add your base directory to the path:

import sys
sys.path.append('../..')

import init_test.a.aaa
...
4
  • This is not enough. Running bbb.py inside b/ will still fail. Commented Sep 12, 2011 at 19:04
  • But then you only can start it if you run python inside init_test/b. Commented Sep 12, 2011 at 19:25
  • yes but that wasn't part of the question :) if it should be called from the base directory then your answer would work
    – cenanozen
    Commented Sep 12, 2011 at 19:30
  • 1
    I updated my answer. Now it should work in all situations, the question gave no information how bbb.py is started... Commented Sep 12, 2011 at 19:35
2

You have to add an empty __init__.py into a. Then a is recognized as a sub package of init_test and can be imported. See http://docs.python.org/tutorial/modules.html#packages

Then change import init_test.a.aaa to import ..a.aaa and it should work. This is -- as Achim says -- a relative import, see http://docs.python.org/whatsnew/2.5.html#pep-328

If you really want to run bbb.py, you have to put init_test/ on your python path, e.g.

import sys
import os

dirname = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(dirname, "../.."))

import sys
sys.path.insert(0, ".")
import init_test.a.aaa
if __name__ == "__main__":
    inittest.a.aaa.test()

And then you can start

python init_test/b/bbb.y

or if you are inside b/

python bbb.py
5
  • import ..a.aaa gives SyntaxError: invalid syntax Commented Sep 12, 2011 at 18:52
  • argh. That does not work, because you run bbb.py inside your package ! Why do you do that ? Commented Sep 12, 2011 at 19:03
  • I tried one outside of the package and it worked. Thanks. Ugh. This whole python multiple files stuff is bullcrap. Why didn't they just set it up similar to C header file linking style... Commented Sep 12, 2011 at 19:58
  • It is no bullcrap if you use packages in the intended way. If you want to test a method in a package, you put the test script outside the package as others would use the package too. Then relative imports work. Manipulating sys.path is a last resort but not the recommended way. Commented Sep 12, 2011 at 20:01
  • No, I realize that might not be the intended way to use a package, but I just mean I wish python would have simple relative path module includes... Commented Sep 12, 2011 at 20:09
0

__init__.py needs to be in all folders that you want to use as modules. In your case, this means init_test/a and init_test/b too.

Not the answer you're looking for? Browse other questions tagged or ask your own question.