Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#20443 closed New feature (fixed)

Import errors in settings cause confusing error messages

Reported by: Sasha Romijn Owned by: nobody
Component: Uncategorized Version: dev
Severity: Normal Keywords: dceu13
Cc: eromijn@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

When the settings file itself has an import error, the error message suggests that the settings file could not be found. This can be very confusing, especially for new users.

Currently, the traceback is:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/erik/dev/django/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/Users/erik/dev/django/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/erik/dev/django/django/core/management/__init__.py", line 263, in fetch_command
    app_name = get_commands()[subcommand]
  File "/Users/erik/dev/django/django/core/management/__init__.py", line 109, in get_commands
    apps = settings.INSTALLED_APPS
  File "/Users/erik/dev/django/django/conf/__init__.py", line 54, in __getattr__
    self._setup(name)
  File "/Users/erik/dev/django/django/conf/__init__.py", line 49, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/erik/dev/django/django/conf/__init__.py", line 130, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'django5622.settings' (Is it on sys.path?): No module named foo

Whereas in reality the settings file was found, but contained an error.

Adding a bit of info on the original traceback makes this very obvious:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/erik/dev/django/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/Users/erik/dev/django/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/erik/dev/django/django/core/management/__init__.py", line 263, in fetch_command
    app_name = get_commands()[subcommand]
  File "/Users/erik/dev/django/django/core/management/__init__.py", line 109, in get_commands
    apps = settings.INSTALLED_APPS
  File "/Users/erik/dev/django/django/conf/__init__.py", line 55, in __getattr__
    self._setup(name)
  File "/Users/erik/dev/django/django/conf/__init__.py", line 50, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/erik/dev/django/django/conf/__init__.py", line 135, in __init__
    raise ImportError(msg)
ImportError: Could not import settings 'django5622.settings' (Is it on sys.path?): No module named o in /Users/erik/dev/django5622/django5622/settings.py:12

Change History (7)

comment:1 by Sasha Romijn, 12 years ago

Has patch: set

comment:2 by svisser, 12 years ago

The error message now kinda assumes the settings module can be found. If not, it'll appear that an error occurred "somewhere in Django":

ImportError: Could not import settings 'djangoproject.settings' (Is it on sys.path?): No module named settings in /Users/simeon/django/django/utils/importlib.py:35

comment:3 by Anssi Kääriäinen, 12 years ago

Triage Stage: UnreviewedAccepted

Maybe the error message could point out that one possible cause is import error in settings file itself?

comment:4 by Sasha Romijn, 12 years ago

Easy pickings: set

@akaariai: yes, that's actually a more sensible and simpler solution. It also fixes svisser's concern. I built the original patch on the assumption that there were more types of errors that caused confusing messages, but it seems to be limited to import errors.

New trivial PR in https://github.com/django/django/pull/1217, tests run.

comment:5 by Chris Wilson, 12 years ago

Triage Stage: AcceptedReady for checkin

Looks good to me.

comment:6 by Erik Romijn <erik@…>, 11 years ago

Resolution: fixed
Status: newclosed

In 9a524e3c5bd8fafd4996a6f1486de8a2cbb052a2:

Fixed #20443 -- Import errors in settings cause confusing error messages

comment:7 by Claude Paroz <claude@…>, 11 years ago

In 1a4b43521883ce85dfbda29b89822a59ffc5220b:

Merge pull request #1217 from erikr/better-settings-errors2

Fixed #20443 -- Import errors in settings cause confusing error messages

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