Opened 6 years ago

Closed 6 years ago

#9482 closed (fixed)

Can't import anything that depends on DANGO_SETTINGS_MODULE in project's `__init__.py` file.

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

Description

I've got some basic code to register signal handlers in my project's __init__.py, as this seems the only place certain (or as close to certain as possible) to execute whenever Django is started with the runfcgi, runserver, shell, etc. management commands.

For example, if I import settings from django.conf, or any other module that relies on settings:

from django.conf import settings
if settings.DEBUG:
    ...

or:

from django.db import models

The following exception is raised:

ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

The reason is because django.core.management.setup_environ adds DJANGO_SETTINGS_MODULE to os.environ *after* importing the project module.

As far as I can tell, the fix is easy. Just move the assignment to os.environ up a few lines, before the import. I can't see how this could possibly break any existing code, nor any reason *not* to do it. We're not adding any code or functionality, just executing it earlier.

Attachments (1)

9482-DJANGO_SETTINGS_MODULE-r9295.diff (1018 bytes) - added by mrmachine 6 years ago.

Download all attachments as: .zip

Change History (3)

comment:1 Changed 6 years ago by mrmachine

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Changed 6 years ago by mrmachine

comment:2 Changed 6 years ago by jacob

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

Fixed in [9843]. Thanks for fucking up my commit message, git!

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