Opened 14 years ago

Closed 14 years ago

Last modified 12 years ago

#16263 closed Bug (needsinfo)

settings __file__ missing for setup_environ()

Reported by: tushar.wagle@… 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 (last modified by Ramiro Morales)

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).

Change History (3)

comment:1 by Bas Peschier, 14 years ago

Resolution: needsinfo
Status: newclosed

I am not completely sure what you called setup_environ with. It looks like you called it with an actual django.conf.Settings-object instead of the module where all settings are placed.

Closing this for now until reporter can further explain the error.

comment:2 by Ramiro Morales, 14 years ago

Description: modified (diff)

comment:3 by deepak.iit@…, 12 years ago

I have the same issue and seems the bug is legit (although the resolution is not clear to me). The fix works fine.

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