Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#20050 closed Uncategorized (needsinfo)

Django models are not importable for utility scripts

Reported by: techtonik Owned by: nobody
Component: Core (Other) Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: yes


application/ often use, and because Django can not import its own settings, an attempt to import models from ordinary command line helper script fails.

My application layout:

+ codereview/
|  +
|  +

from codereview import models

And the error:

  File "codereview\", line 676, in Account
    default_context = db.IntegerProperty(default=settings.DEFAULT_CONTEXT,
  File "...\django-1.4.5-py2.7.egg\django\utils\", line 184, in inner
  File "...\django-1.4.5-py2.7.egg\django\conf\", line 40, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

I know about os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' workaround. The questions are:

  • Why users need to specify it every time explicitly?
  • Why 'settings' is not the sane default?
  • Why this error can not be shown only after Django actually fails to find the settings?

Change History (3)

comment:1 Changed 3 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

This looks like a usage question rather than a bug report :)

Since Django 1.4, the canonical value for DJANGO_SETTINGS_MODULE is <myapp>.settings and not settings. settings works for you because you're using a non-standard layout, but it isn't a sane default in general.

Please review the discussions on django-developers about the new project layout implemented in 1.4 for more information.

If you'd like to provide an alternative, please write to the django-developers mailing-list. This is the appropriate medium to discuss new features or refactorings.

comment:2 Changed 3 years ago by techtonik

Can you share these links? Because this answer doesn't explain anything. =)

For a feature request, why not to display sane user message directing where to find this DJANGO_SETTINGS_MODULE?
Failure to specify config is not an application level error, so no need to throw up with cryptic stacktraces.

comment:3 Changed 3 years ago by techtonik

  • Resolution changed from invalid to needsinfo

I also don't understand what is the problem to check package root directory for settings if settings inside package are not found?

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