Opened 7 years ago

Closed 5 years ago

Last modified 5 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 7 years ago.
use traceback.format_exc()
django_11667_load_middleware.diff (1.2 KB) - added by jedie 7 years ago.
patch for #comment:3
django_11667_manage_test.diff (902 bytes) - added by Allen Riddell 7 years ago.
patch for #comment:4
remove_try_catch (992 bytes) - added by Klaas van Schelven 6 years ago.
A patch that drops the try/catch block altogether

Download all attachments as: .zip

Change History (19)

Changed 7 years ago by jedie

Attachment: django_11667_patch.diff added

use traceback.format_exc()

comment:1 Changed 7 years ago by jedie

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

comment:2 Changed 7 years ago by Alex Gaynor

Triage Stage: UnreviewedAccepted

comment:3 Changed 7 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 7 years ago by jedie

patch for #comment:3

comment:4 Changed 7 years ago by Allen Riddell

Owner: changed from nobody to Allen Riddell
Status: newassigned

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 7 years ago by Allen Riddell

Cc: riddell@… added

comment:6 Changed 7 years ago by Allen Riddell

Owner: Allen Riddell deleted
Status: assignednew

comment:7 Changed 7 years ago by Allen Riddell

Owner: set to nobody

Changed 7 years ago by Allen Riddell

patch for #comment:4

comment:8 Changed 6 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 6 years ago by Klaas van Schelven

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 6 years ago by Klaas van Schelven

Attachment: remove_try_catch added

A patch that drops the try/catch block altogether

comment:10 Changed 6 years ago by Klaas van Schelven

Cc: klaasvanschelven@… added

comment:11 Changed 6 years ago by Gabriel Hurley

Component: django-admin.py runserverCore (Management commands)

comment:12 Changed 5 years ago by Julien Phalip

Severity: Normal
Type: New feature

comment:13 Changed 5 years ago by anonymous

Easy pickings: unset
UI/UX: unset

Thanks so much!!

comment:14 Changed 5 years ago by Aymeric Augustin

Resolution: duplicate
Status: newclosed

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 5 years ago by Aymeric Augustin

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