2

I have a circular import problem:

 File "/Library/Python/2.7/site-packages/django/db/models/loading.py", line 96, in load_app
    models = import_module('.models', app_name)

 File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)

  File "/Users/......../account/models.py", line 11, in <module>
    from account import model_managers as model_mgrs

  File "/Users/......../account/model_managers.py", line 6, in <module>
    from account import models as account_models
ImportError: cannot import name models

I followed this guy's recommendation to deal with circular imports by only importing the module: https://stackoverflow.com/a/3956038/1724763

But I still got an error. What now?

UPDATED

OK, I solved the problem by doing in account/model_managers.py:

import importlib
account_models = importlib.import_module('.models', 'account')

Although it does look unwieldy. Not sure whether it is pythonic...

Marco
  • 1,377
  • 15
  • 18
  • Could you post the import statements of the different files? – Hyperboreus Aug 18 '13 at 06:55
  • Yes, `__import__(name)`. What is `name` referring to at the moment the error occurs? – Hyperboreus Aug 18 '13 at 07:09
  • Just put a `print (name)` before line 35 in /Library/Python/2.7/site-packages/django/utils/importlib.py. – Hyperboreus Aug 18 '13 at 07:13
  • Possible duplicate of [What is the difference between "import pkg.a as a" and "from pkg import a"?](https://stackoverflow.com/questions/49784828/what-is-the-difference-between-import-pkg-a-as-a-and-from-pkg-import-a) – Davis Herring Dec 21 '18 at 18:55

0 Answers0