Opened 6 years ago

Closed 5 years ago

#11633 closed (worksforme)

Stop requiring DJANGO_SETTINGS_MODULE when using settings.configure

Reported by: masklinn Owned by: nobody
Component: Core (Other) Version: master
Severity: Keywords: settings
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Currently, django requires both that there is a DJANGO_SETTINGS_MODULE environment variable and that it points to an actual module, even though it's not necessary when calling django.config.settings.configure.

Therefore to use Django modules in standalone system (or when testing an application on its own without a project, as in #11593) one has to either create an empty settings file (which kind-of defeats the point of using settings.configure in the first place) or programmatically create a DJANGO_SETTINGS_MODULE environ set to a dummy module name and generate the corresponding dummy module in settings.modules e.g.

os.environ['DJANGO_SETTINGS_MODULE'] = "durrrrr"
sys.modules['durrrrr'] = type('IMA_MODULE', (), {})()
from django.conf import settings
# call settings.configure() here

Now while this isn't exactly hard to do (if you understand how module imports work & everything), it's annoying to have to do it and it can conflict with other modules if the dummy name isn't correctly chosen. It's also a waste of time on the user's side.

Change History (4)

comment:1 Changed 6 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 5 years ago by niall

This doesn't appear to be the case anymore:

import os
from django.conf import settings
print os.environ.get("DJANGO_SETTINGS_MODULE", None)

None

settings.configure({"foo": "bar"})
settings.foo

'bar'

comment:3 Changed 5 years ago by niall

This doesn't appear to be the case anymore:

>>> import os
>>> from django.conf import settings
>>> print os.environ.get("DJANGO_SETTINGS_MODULE", None)
None
>>> settings.configure(**{"foo": "bar"})
>>> settings.foo
'bar'

Sorry for the double post, formatting was messed up the first time.

comment:4 Changed 5 years ago by niall

  • Resolution set to worksforme
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.
Back to Top