Problem with python 3.2 and circular imports

Rafael Durán Castañeda rafadurancastaneda at gmail.com
Mon Feb 28 14:23:53 EST 2011


I'm stil totally stuck with relative imports, i' ve tried the example tree
from PEP 328 without any result:

package/
    __init__.py
    subpackage1/
        __init__.py
        moduleX.py
        moduleY.py
    subpackage2/
        __init__.py
        moduleZ.py
    moduleA.py

Assuming that the current file is either moduleX.py or
subpackage1/__init__.py, following are correct usages of the new
syntax:

from .moduleY import spam
from .moduleY import spam as ham
from . import moduleY
from ..subpackage1 import moduleY
from ..subpackage2.moduleZ import eggs
from ..moduleA import foo
from ...package import bar
from ...sys import path

I always get:

Traceback (most recent call last):
  File "moduleY.py", line 1, in <module>
    from ..moduleA import a
ValueError: Attempted relative import in non-package


2011/2/27 Frank Millman <frank at chagford.com>

>
> "Steven D'Aprano" <steve+comp.lang.python at pearwood.info> wrote in message
> news:4d6a56aa$0$29972$c3e8da3$5496439d at news.astraweb.com...
>
>  On Sun, 27 Feb 2011 12:08:12 +0200, Frank Millman wrote:
>>
>>  Assume the following structure -
>>>
>>> main.py
>>> /pkg
>>>    __init__.py
>>>    mod1.py
>>>    mod2.py
>>>
>>> main.py
>>>    from pkg import mod1
>>>
>>> mod1.py
>>>    import mod2
>>>
>>> mod2.py
>>>  import mod1
>>>
>>
>>
>> If you change the "import mod*" lines to "import pkg.mod*" it works for
>> me in Python 3.1 and 3.2.
>>
>> According to my understand of PEP 328, "from . import mod*" should work,
>> but I agree with you that it doesn't.
>>
>> If you get rid of the circular import, it does work. So I suspect a bug.
>>
>>
>>
> Thanks, Steven.
>
> I confirm that 'import pkg.mod* works. Unfortunately I am using
> sub-packages as well, which means that to refer to an object in the
> sub-package I need to use w.x.y.z every time, which gets to be a lot of
> typing! I will stick to my hack of putting the package name in sys.path for
> now, unless someone comes up with a better idea.
>
> Frank
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110228/11950906/attachment-0001.html>


More information about the Python-list mailing list