I have following directory structure:
- /
- package/
- subpackage/
- __init__.py
- file.py
- __init__.py
- file.py
- subpackage/
- main.py
- package/
/package/subpackage/__init__.py has following code:
from .. import file;
It imports /package/file.py as expected.
/main.py has following code:
from package import subpackage as foo;
from package.subpackage import file as bar;
Last line imports /package/file.py, not /package/subpackage/file.py. bar.__name__ confirms it. Why? What’s wrong?
Python 2.5.2. Each file has
from __future__ import absolute_import;
at beginning.
Nothing is wrong, it does exactly what you told it to:
When you import
package.subpackage, you’re executing/package/subpackage/__init__.py. And there you dofrom .. import file. Sofileinpackage.subpackageispackage.file.