0

I observed an unexcepted(imo) behaviour in python2.7. Program structure looks like this:

run.py
package/
    __init__.py
    a.py
    b.py

run.py:

from package import *

init.py:

from package.a import *

Consider now a.py and b.py as empty files.

In my opinion, this program should fail due circural import. Why? Because when we import package init file has an absolute import which indicates to access same init file while importing.

To see excepted by me situation just add to a.py this:

from package.b import *

In result we receive an ImportError.

It is a bug, or excepted behaviour? I can't find any information in docs, about special treated absolute imports in init files?

badray
  • 300
  • 2
  • 8
  • This has nothing to do with absolute or relative imports. – User Jul 23 '14 at 21:35
  • Please be more specific. For me, this is an absolut import. – badray Jul 24 '14 at 09:39
  • @User's point is that the behavior would be the same, regardless of if you used absolute or relative importing. See this: http://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python – aruisdante Jul 24 '14 at 13:53

1 Answers1

0

I've just tried to reproduce same case, and I realized that in the day I was posting question, I was extremely tired. Question is invalid :). You guys were right that this has nothing to do with absolute imports.

__init__.py is loaded only once like any other module while importing and this is excepted behaviour. So in case when I have a statement like from package.a import * in __init__.py __init__.py is already in sys.modules (with name inherited from folder, in this case package) and it is not loaded again.

Circural exception will occures only when we try to use a variable from module that wasn't arleady initialized. So for example if I put in a.py from package import * interpreter will terminate with exception becouse __init__ is not fully initialized.

badray
  • 300
  • 2
  • 8