سؤال

I made some changes to my code, adding a model, and added to some imports and now all of a sudden when I try to run a few management style commands I've scripted they fail with the following traceback:

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

I've tried adding try/except blocks around the imports in the jobs.py file (that defines the command line commands), the views.py, the admins.py and the crrs.py module that has the function I'm trying to run. When I try/except them all out the script works but fails because the models aren't defined anywhere.

As well, when I use shell_plus and import the job file or the crrs file directly I can execute the function I want to without any error. I've stepped through all of the code that was changed between commits and there's no obvious reason for this problem.

Any insight would be appreciated!

هل كانت مفيدة؟

المحلول

Your Company model, via a whole wack of imports, relies on your urls.py, which relies on admin.py, which relies on Company. So you have a circular import.

I suggest attempting to resolve this by commenting out admin.autodiscover(), but showing your django version would help determine if this is actually the culprit.

Given the version of Django is < 1.7, autodiscover() still recommended. Next thing to check is if the django-debug-toolbar auto patching is the problem, see the installation instructions especially the part called 'WARNING...may cause circular imports'.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top