[Python-Dev] intra-package mutual imports fail: "from <pkg> import <mod>"

Guido van Rossum guido@python.org
Sat, 01 Jun 2002 00:42:42 -0400


> However, if I change both imports to be absolute using the
> "from/import" form, it doesn't work::
> 
>         module5.py:
>             from package import module6   # absolute import
> 
>         module6.py:
>             from package import module5
> 
> Now I get an exception::
> 
>     >>> from package import module5
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in ?
>       File "package/module5.py", line 1, in ?
>         from package import module6
>       File "package/module6.py", line 1, in ?
>         from package import module5
>     ImportError: cannot import name module5
> 
> Is this behavior expected?  Or is it a bug?

It's probably due to the extremely subtle (lame?) way that "from
package import module" is (has to be?) implemented.

It's too late at night for me to dig further to come up with an
explanation, but maybe reading the file knee.py is helpful -- it gives
the *algorithm* used for package and module import.  In 2.2 and
before, it's Lib/knee.py; in 2.3, it's been moved to
Demo/imputils/knee.py.

I think that you'll have to live with it.

--Guido van Rossum (home page: http://www.python.org/~guido/)