This may be python's desired behavior, but I find it puzzling nonetheless -
I have the following file structure:
repo/
foo/
__init__.py
foo/
bar.py
where __init__.py
is the following:
from .foo import bar
When I enter a python interpreter from within repo/
, I am able to import bar
like so:
>>> from foo import bar
>>> bar
<module 'foo.foo.bar' from '.../repo/foo/foo/bar.py'>
Great! However, the following does not work:
>>> import foo.bar as foobar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'foo.bar'
Perhaps I lack an understanding of how python's import statement works, but I would very much like to be able import as I showed above. If this is desired behavior, does anyone know of a workaround? Perhaps I can change something in __init__.py
?
Out of concern for naming caveats, I have also tested this with the following file structure:
repo/
foo/
__init__.py
baz/
bar.py
... and I got the same result
I've tested this in python 3.6.2 and 3.6.6 on linux and macOS. I also saw the same behavior in python 2.7.13 (where, of course, I added an __init__.py
into the lower foo/
directory.
If I am unable to make this style of import work, repo/
will have to undergo a bit of refactoring. Help me prevent this!