izstas
izstas

Reputation: 5064

Python - Importing file from subpackage

I have following directory structure:

/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.

Upvotes: 3

Views: 2223

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799420

Because it imports file from .., just as your first snippet says.

Upvotes: 0

vartec
vartec

Reputation: 134711

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 do from .. import file. So file in package.subpackage is package.file.

Upvotes: 6

Related Questions