Python circular import

时光总嘲笑我的痴心妄想 提交于 2019-12-23 01:52:26

问题


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...

来源:https://stackoverflow.com/questions/18296488/python-circular-import

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!