Opened 6 years ago

Closed 3 years ago

Last modified 3 years ago

#11667 closed New feature (duplicate)

Full traceback if import error before model validation

Reported by: jedie Owned by: nobody
Component: Core (Management commands) Version: 1.1
Severity: Normal Keywords:
Cc: django@…, riddell@…, klaasvanschelven@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I had an import loop in a app model. If i call the management command "runserver", i get only a one line error message, whitch doesn't help to find the real error.

django.core.management.base.execute should display a read traceback. See patch.

Attachments (4)

django_11667_patch.diff (731 bytes) - added by jedie 6 years ago.
use traceback.format_exc()
django_11667_load_middleware.diff (1.2 KB) - added by jedie 6 years ago.
patch for #comment:3
django_11667_manage_test.diff (902 bytes) - added by ariddell 5 years ago.
patch for #comment:4
remove_try_catch (992 bytes) - added by vanschelven 4 years ago.
A patch that drops the try/catch block altogether

Download all attachments as: .zip

Change History (19)

Changed 6 years ago by jedie

use traceback.format_exc()

comment:1 Changed 6 years ago by jedie

  • Cc django@… added
  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset

comment:2 Changed 6 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 6 years ago by jedie

django.core.management.base.BaseHandler.load_middleware will also "mask" some import errors. Add a separate patch, if settings.DEBUG is on, raise the original error instead of ImproperlyConfigured, so the user will get a helpfull traceback.

Changed 6 years ago by jedie

patch for #comment:3

comment:4 Changed 5 years ago by ariddell

  • Owner changed from nobody to ariddell
  • Status changed from new to assigned

I had a similar issue when running tests. I would do a manage.py test and get an django.core.exceptions.ImproperlyConfigured: App with label <label> could not be found. The real culprit was an import loop. As jedie suggests, displaying the original ImportError would be helpful.

I'll attached a patch for the manage.py test case as django_11667_manage_test.diff.

comment:5 Changed 5 years ago by ariddell

  • Cc riddell@… added

comment:6 Changed 5 years ago by ariddell

  • Owner ariddell deleted
  • Status changed from assigned to new

comment:7 Changed 5 years ago by ariddell

  • Owner set to nobody

Changed 5 years ago by ariddell

patch for #comment:4

comment:8 Changed 5 years ago by peterlundberg

I agree that wrapping the exceptions makes debugging import errors very hard and would be Good Thing to improve with Django

I have on occasion replaced

raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))

with just

raise

in template/loaders/app_directories.py line 24 (django 1.2.1) which allows me to see what module/file was trying to do the failing import when running manage.py test

A message like 'cannot import %s from %s line %d' from ImportError would help a lot, but that is more a python issue.

comment:9 Changed 4 years ago by vanschelven

Any movement on this?

I can't count the number of times I've dived into the Django code and added a 'raise'. Of course, the proper solution would be simply to drop the associated try/catch block, since all it does is obfuscate things.

This can be rewritten:

206         if self.can_import_settings:
207             try:
208                 from django.utils import translation
209                 translation.activate('en-us')
210             except ImportError, e:
211                 # If settings should be available, but aren't,
212                 # raise the error and quit.
213                 sys.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
214                 sys.exit(1)

I'll submit a patch in a sec.

Changed 4 years ago by vanschelven

A patch that drops the try/catch block altogether

comment:10 Changed 4 years ago by vanschelven

  • Cc klaasvanschelven@… added

comment:11 Changed 4 years ago by gabrielhurley

  • Component changed from django-admin.py runserver to Core (Management commands)

comment:12 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to New feature

comment:13 Changed 4 years ago by anonymous

  • Easy pickings unset
  • UI/UX unset

Thanks so much!!

comment:14 Changed 3 years ago by aaugustin

  • Resolution set to duplicate
  • Status changed from new to closed

I'm closing this ticket in favor of #16397 which reports the same problem (as far as I can tell) and has a more recent patch.

comment:15 Changed 3 years ago by aaugustin

In [17197]:

Fixed #16397 -- Respected the --traceback flag in BaseCommand. This should make import loops easier to debug. Refs #11667, #17369.

Note: See TracTickets for help on using tickets.
Back to Top