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
You have to add an empty
__init__.pyinto a. Then a is recognized as a sub package of init_test and can be imported. See http://docs.python.org/tutorial/modules.html#packagesThen change
import init_test.a.aaatoimport ..a.aaaand it should work. This is — as Achim says — a relative import, see http://docs.python.org/whatsnew/2.5.html#pep-328If you really want to run
bbb.py, you have to put init_test/ on your python path, e.g.And then you can start
or if you are inside b/