Leopd
Leopd

Reputation: 42767

Bizarre inconsistent python ImportError - possible circular dependency?

I'm trying to refactor some python code and I'm stuck with an import error I don't understand. I suspect there might be a circular dependency somewhere but I don't see it, and I'm not getting much in the way of hints from the error messages. The codebase is large, but there are two modules of interest here:

radian/models.py defines a class called ACount

datalayer/radian.py has the following line in it:

from radian.models import ACount

When I run the code (either interactively or from the main program) the imports fail in a way that doesn't make sense to me.

>>> from radian.models import ACount
>>> import datalayer.radian
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/leopd/dev/dbproj/datalayer/radian.py", line 10, in <module>
    from radian.models import ACount
ImportError: No module named models

My best guess is that there's a circular dependency somewhere -- that radian is importing something that imports datalayer. But I don't see it. And the error message doesn't make any sense to me. Any ideas what's going on?

-- UPDATE --

I'm using python 2.6.1 on Mac. The __init__.py files have some code in them, but they only import from standard python packages.

Upvotes: 2

Views: 309

Answers (1)

Vinay Sajip
Vinay Sajip

Reputation: 99405

Any module in the datalayer folder (including radian.py), when it sees from radian, will assume that datalayer/radian.py is the relevant module. You might need to do

from __future__ import absolute_import

in datalayer/radian.py and other similarly affected modules, and then check all your imports to ensure that they're absolute. You may be able to get away with renaming datalayer/radian.py and the imports which reference it, depending on where that module is referenced from.

Upvotes: 1

Related Questions