[Python-3000] cyclic imports worked in py2.6, fail in 3.0
Ondrej Certik
ondrej at certik.cz
Tue Nov 11 13:15:00 CET 2008
>> What it does is that it injects the classes defined in this particular
>> module to the other modules. First problem: 2to3 tool doesn't convert
>> this correctly, I had to manually conver this
>> to:
>
> Would you file a bug report for the 2to3 problem, please? That should be fixed.
I will. There are a lot more import conversions problem like this one.
>> However, this works in python2.4, 2.5 and 2.6. Notice, that "from .
>> import mul as _" worked in power.py, but failed in add.py 3 lines
>> below. This is weird, isn't it?
>
> Actually, if you use the relative imports with 2.6, it fails like 3.0.
> 3.0 is just being stricter.
>
>>
>> So my questions are:
>>
>> * is our "hack" supported at all? If not, how would you suggest us to
>> handle cyclic imports? Basically, we want Add and Mul classes to be
>> defined in separate modules, however the methods of both classes need
>> access to the other --- so the only other option that I can see is to
>> locally import the other module in each method, which is slow and not
>> so clean. Another option is to import the other class to the module at
>> runtime using some dynamic features of Python.
>
> First, I suggest instead of using sibling imports in your packages,
> you should convert to all relative imports or all absolute imports.
> (ie. from sympy.core import something)
The problem is that we still need to support python2.4, so the only
option seems to be absolute imports. I think from the major
distributions, only Debian and Gentoo still use 2.4 in their stable
versions, but everyone now uses 2.5 in their unstable versions, so I
guess we need to support 2.4 for at least one more year or two.
>
> Instead of inserting Mul into the namespace of different modules, you
> do something like:
>
> from .mul import Mul
>
> at the bottom of files that use the cyclic import.
Ok, I'll try to fix it this way.
>
>>
>> * if it is supposed to work, is this a bug in python3.0?
>
> No, Python 3.0 is just being stricter. :) You may want to test this
> out by using "from __future__ import absolute_import".
Thanks for the info. So I'll first convert the imports, then run it
through 2to3 again and report all problems again. I noticed, that no
imports from sympy/__init__.py and all the other __init__py files were
converted and they fail with python3.0.
I must admit, that because I was still using python2.4, I must first
learn how it works (e.g. naively adding the dot like "from .functions
import *" sometimes fail) and then report back when I understand it
more.
Ondrej
More information about the Python-3000
mailing list