【问题标题】:Django cannot import LOCAL settingsDjango 无法导入本地设置
【发布时间】:2014-05-19 05:04:34
【问题描述】:

编辑: 使用 Django 1.7... 和 Python 3

在使用 manage.py 时,我似乎无法将 local_settings.py 导入到我的 settings.py 文件中。

如果我直接执行 settings.py 文件,我的 local_settings.py 文件导入正常,没有任何错误。

但是,当我运行 manage.py 时,它抱怨找不到 local_settings.py 模块。 settings.py 和 local_settings.py 在同一个文件夹中...

有什么想法吗?

Traceback (most recent call last):
  File "/home/cg/webdev/riv_com/lib/python3.4/site-packages/django/conf/__init__.py", line 94, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.4/importlib/__init__.py", line 104, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2203, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1448, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/cg/webdev/riv_com/riv_com/riv_com/settings.py", line 79, in <module>
    from local_settings import *
ImportError: No module named 'local_settings'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/cg/webdev/riv_com/lib/python3.4/site-packages/django/core/management/__init__.py", line 427, in execute_from_command_line
    utility.execute()
  File "/home/cg/webdev/riv_com/lib/python3.4/site-packages/django/core/management/__init__.py", line 386, in execute
    settings.INSTALLED_APPS
  File "/home/cg/webdev/riv_com/lib/python3.4/site-packages/django/conf/__init__.py", line 46, in __getattr__
    self._setup(name)
  File "/home/cg/webdev/riv_com/lib/python3.4/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/cg/webdev/riv_com/lib/python3.4/site-packages/django/conf/__init__.py", line 98, in __init__
    % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'riv_com.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named 'local_settings'

编辑:

这个错误似乎只出现在Python3.4

我已经测试过:

Django 1.6 + Python3.4 = 错误
Django 1.7 + Python3.4 = 错误

Django 1.6 + Python2.7 = 好的

【问题讨论】:

    标签: python django settings


    【解决方案1】:

    Python 3.4 不支持隐式相对导入:Python 3 中的from local_settings import * 是一个绝对导入,并且只会在您的sys.path 中搜索local_settings 模块,但不在同一个模块中settings.py 模块所在的目录。您需要使用显式相对导入:from .local_settings import *;这也适用于 Python 2.7。

    参考:PEP 328

    【讨论】:

      【解决方案2】:

      在settings.py中

      import os
      SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
      
      DEBUG = False
      # delete TEMPLATE_DEBUG = DEBUG
      

      在settings.py的底部

      ##################
      # LOCAL SETTINGS #
      ##################
      
      # Allow any settings to be defined in local_settings.py which should be
      # ignored in your version control system allowing for settings to be
      # defined per machine.
      try:
          from local_settings import *
      except ImportError:
          pass
      

      在 loacal_settings.py 中

      # Grabs the site root setup in settings.py
      # import os
      # from settings import SITE_ROOT
      
      from settings import *
      
      DEBUG = True
      TEMPLATE_DEBUG = DEBUG
      
      # sqlite is the quick an easy development db
      DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(SITE_ROOT, 'djlocal.db'),
            'USER': '',             # Not used with sqlite3.
            'PASSWORD': '',         # Not used with sqlite3.
            'HOST': '',             # Not used with sqlite3.
            'PORT': '',             # Not used with sqlite3.
        }
      }
      

      【讨论】:

        【解决方案3】:

        lanzz 的答案在描述 settings.py 底部的部分有一个错误:它说

        from local_settings import * 
        

        缺少相对导入的点。应该是

        from .local_settings import *
        

        【讨论】:

          猜你喜欢
          • 2014-06-06
          • 1970-01-01
          • 1970-01-01
          • 2014-02-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多