Cyclic relative imports don't work

Torsten Bronger bronger at physik.rwth-aachen.de
Thu Apr 10 12:49:50 EDT 2008


Hallöchen!

Assume the follwing package structure:

main.py
package/
    __init__.py   [empty]
    moduleX.py
    moduleY.py

main.py says:

    from package import moduleX

moduleX.py says:

    from . import moduleY

and moduleY.py says:

    from . import moduleX

However, this doesn't work:

    bronger at wilson:~/temp/packages-test$ python main.py
    Traceback (most recent call last):
      File "main.py", line 1, in <module>
        from package import moduleX
      File "/home/bronger/temp/packages-test/package/moduleX.py", line 1, in <module>
        from . import moduleY
      File "/home/bronger/temp/packages-test/package/moduleY.py", line 1, in <module>
        from . import moduleX
    ImportError: cannot import name moduleX

If I turn the relative imports to absolute ones, it works.  But I'd
prefer the relative notation for intra-package imports.  Why is this
restriction?

Tschö,
Torsten.

-- 
Torsten Bronger, aquisgrana, europa vetus
                                      Jabber ID: bronger at jabber.org
               (See http://ime.webhop.org for further contact info.)



More information about the Python-list mailing list