Opened 7 years ago

Closed 7 years ago

Last modified 6 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 "", line 10, in <module>
  File "c:\Python27\lib\site-packages\django\core\management\", line 394, in setup_environ
    if '' in settings_mod.__file__:
  File "c:\Python27\lib\site-packages\django\utils\", 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 import setup_environ

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

            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, but this isn't ideal.

(Also note that this is not a problem when using, just when trying to use this standalone, eg in a non-webserver process).

Change History (3)

comment:1 Changed 7 years ago by Bas Peschier

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 Changed 7 years ago by Ramiro Morales

Description: modified (diff)

comment:3 Changed 6 years ago by deepak.iit@…

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