可能的导入循环?

时间:2014-05-14 22:04:14

标签: python django python-import

我对我的代码进行了一些更改,添加了一个模型,并添加了一些导入,现在突然间,当我尝试运行一些管理样式命令时,我已经编写了脚本,但它们失败并带有以下回溯:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 272, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/usr/lib/python2.6/site-packages/django/core/management/__init__.py", line 75, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/usr/lib/python2.6/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
  File "/home/site/contracts/core/management/commands/job.py", line 13, in <module>
    from contracts import jobs
  File "/home/site/contracts/jobs.py", line 8, in <module>
    from contracts.imports import crrs, crrs_bids
  File "/home/site/contracts/imports/crrs.py", line 17, in <module>
    from contracts.core.models import Company, Contract, Bid, Department, Category
  File "/home/site/contracts/core/models.py", line 26, in <module>
    class Company(models.Model):
  File "/usr/lib/python2.6/site-packages/django/db/models/base.py", line 243, in __new__
    new_class._prepare()
  File "/usr/lib/python2.6/site-packages/django/db/models/base.py", line 307, in _prepare
    signals.class_prepared.send(sender=cls)
  File "/usr/lib/python2.6/site-packages/django/dispatch/dispatcher.py", line 185, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/usr/lib/python2.6/site-packages/simple_history/models.py", line 50, in finalize
    history_model = self.create_history_model(sender)
  File "/usr/lib/python2.6/site-packages/simple_history/models.py", line 77, in create_history_model
    app = models.get_app(model._meta.app_label)
  File "/usr/lib/python2.6/site-packages/django/db/models/loading.py", line 179, in get_app
    self._populate()
  File "/usr/lib/python2.6/site-packages/django/db/models/loading.py", line 78, in _populate
    self.load_app(app_name)
  File "/usr/lib/python2.6/site-packages/django/db/models/loading.py", line 99, in load_app
    models = import_module('%s.models' % app_name)
  File "/usr/lib/python2.6/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
  File "/usr/lib/python2.6/site-packages/debug_toolbar/models.py", line 9, in <module>
    dt_settings.patch_all()
  File "/usr/lib/python2.6/site-packages/debug_toolbar/settings.py", line 215, in patch_all
    patch_root_urlconf()
  File "/usr/lib/python2.6/site-packages/debug_toolbar/settings.py", line 203, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 503, in reverse
    app_list = resolver.app_dict[ns]
  File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 329, in app_dict
    self._populate()
  File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 267, in _populate
    for pattern in reversed(self.url_patterns):
  File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 365, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 360, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/usr/lib/python2.6/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
  File "/home/site/contracts/urls.py", line 11, in <module>
    admin.autodiscover()
  File "/usr/lib/python2.6/site-packages/django/contrib/admin/__init__.py", line 29, in autodiscover
    import_module('%s.admin' % app)
  File "/usr/lib/python2.6/site-packages/django/utils/importlib.py", line 40, in import_module
    __import__(name)
  File "/home/site/contracts/core/admin.py", line 3, in <module>
    from contracts.core.models import Company, Contract, Bid, Department, Category
ImportError: cannot import name Company

我尝试在jobs.py文件(定义命令行命令),views.py,admins.py和具有函数I的crrs.py模块中的导入周围添加try / except块我想跑。当我尝试/除了它们以外,脚本全部工作但失败,因为模型没有在任何地方定义。

同样,当我使用shell_plus并直接导入作业文件或crrs文件时,我可以执行我想要的功能而不会出现任何错误。我已经介绍了在提交之间更改的所有代码,并且没有明显的原因导致此问题。

任何见解都将受到赞赏!

1 个答案:

答案 0 :(得分:2)

您的公司模式,通过一大堆进口,依赖于您的urls.py,它依赖于依赖于公司的admin.py。所以你有一个循环导入。

我建议尝试通过注释掉admin.autodiscover()来解决这个问题,但是显示你的django版本将有助于确定这是否真的是罪魁祸首。

鉴于Django的版本是&lt; 1.7,自动发现()仍然推荐。接下来要检查的是django-debug-toolbar自动修补是否有问题,请参阅installation instructions,特别是名为&#39; WARNING ...的部分可能导致循环导入&#39;。