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

Asclepius
64.7k20 gold badges188 silver badges165 bronze badges
asked Sep 12, 2011 at 18:42
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
...
answered Sep 12, 2011 at 18:45
Sign up to request clarification or add additional context in comments.

4 Comments

This is not enough. Running bbb.py inside b/ will still fail.
But then you only can start it if you run python inside init_test/b.
yes but that wasn't part of the question :) if it should be called from the base directory then your answer would work
I updated my answer. Now it should work in all situations, the question gave no information how bbb.py is started...
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
Asclepius
64.7k20 gold badges188 silver badges165 bronze badges
answered Sep 12, 2011 at 18:46

5 Comments

import ..a.aaa gives SyntaxError: invalid syntax
argh. That does not work, because you run bbb.py inside your package ! Why do you do that ?
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...
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.
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...
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.

answered Sep 12, 2011 at 18:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.