Django

Code

Ticket #9482 (closed: fixed)

Opened 1 year ago

Last modified 1 year ago

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

Reported by: mrmachine Assigned to: nobody
Milestone: Component: Core framework
Version: 1.0 Keywords: DJANGO_SETTINGS_MODULE settings import
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

9482-DJANGO_SETTINGS_MODULE-r9295.diff (1.0 kB) - added by mrmachine on 10/31/08 01:40:54.

Change History

10/31/08 00:45:58 changed by mrmachine

  • needs_better_patch changed.
  • has_patch set to 1.
  • needs_tests changed.
  • needs_docs changed.

10/31/08 01:40:54 changed by mrmachine

  • attachment 9482-DJANGO_SETTINGS_MODULE-r9295.diff added.

02/16/09 15:19:10 changed by jacob

  • status changed from new to closed.
  • resolution set to fixed.

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


Add/Change #9482 (Can't import anything that depends on DANGO_SETTINGS_MODULE in project's `__init__.py` file.)




Change Properties
Action