I'm confused on why a simple absolute import is failing. Following the Python packages tutorial, I have package with a single subpackage:
sound/
__init__.py
top.py
formats/
__init__.py
a.py
b.py
a.py contains:
def foo():
print("foo")
b.py contains:
from a import foo
def bar():
foo()
if __name__ == "__main__":
bar()
top.py contains:
from formats import b
if __name__ == "__main__":
b.bar()
Both __init__.py
files are empty. From sound/formats/, running b prints foo
as expected. But from sound/, running top produces the error:
File ".../sound/top.py", line 1, in <module>
from formats import b
File "...\sound\format\b.py", line 1, in <module>
from a import foo
ImportError: No module named 'a'
(Note the strange appearance of forward slashes in the first line and back slashes in the second. Python 3.5, Windows 7 Pro.) This shouldn't be that complicated -- what syntax is necessary to allow b to consistently import a?
----- EDIT -----
Running unittest is the question I should have found before asking this one. It also contains a great pointer to the Python Project Howto.