Opened 13 years ago
Last modified 12 years ago
#16263 closed Bug
settings __file__ missing for setup_environ() — at Initial Version
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Core (Other) | Version: | 1.3 |
Severity: | Normal | Keywords: | conf, settings |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
I'm getting the following problem:
---
Traceback (most recent call last):
File "request_handler.py", line 10, in <module>
setup_environ(settings)
File "c:\Python27\lib\site-packages\django\core\management\init.py", line 394, in setup_environ
if 'init.py' in settings_mod.file:
File "c:\Python27\lib\site-packages\django\utils\functional.py", line 277, in getattr
return getattr(self._wrapped, name)
AttributeError: 'Settings' object has no attribute 'file'
---
when trying to use django standalone with code like this (and with DJANGO_SETTINGS_MODULE set to a valid module):
from django.conf import settings
from django.core.management import setup_environ
setup_environ(settings)
The problem is that there is no file attribute on the django.conf.Settings object, and the setup_environ() code assumes that there will be.
One fix (that I've tested) is to add the copy of the file attribute within the Settings class in django.conf.init.py like this:
---
class Settings(BaseSettings):
def init(self, settings_module):
# update this dict from global settings (but only for ALL_CAPS settings)
for setting in dir(global_settings):
if setting == setting.upper():
setattr(self, setting, getattr(global_settings, setting))
# store the settings module in case someone later cares
self.SETTINGS_MODULE = settings_module
try:
mod = importlib.import_module(self.SETTINGS_MODULE)
except ImportError, e:
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
# Settings that should be converted into tuples if they're mistakenly entered
# as strings.
tuple_settings = ("INSTALLED_APPS", "TEMPLATE_DIRS")
for setting in dir(mod):
#if setting == setting.upper(): ## OLD BROKEN
if setting == setting.upper() or setting == 'file': #NEW FIXED
---
This works, but might not be the best solution. This problem means that I cannot use any settings files other than the default one (so is quite a big problem). There is of course a simple workaround: ie to rename my custom file to settings.py, but this isn't ideal.
(Also note that this is not a problem when using manage.py, just when trying to use this standalone, eg in a non-webserver process).