Django无法导入本地设置

9 投票
3 回答
13174 浏览
提问于 2025-04-18 01:40

编辑:

我在玩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'

编辑:

这个错误似乎只在Python 3.4中出现。

我测试过:

Django 1.6 + Python 3.4 = 出错
Django 1.7 + Python 3.4 = 出错

Django 1.6 + Python 2.7 = 没问题

3 个回答

0

lanzz的回答在描述settings.py文件底部的部分有一个错误:那里说

from local_settings import * 

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

from .local_settings import *
7

在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.
  }
}
25

Python 3.4 不支持隐式的相对导入:在 Python 3 中,像 from local_settings import * 这样的写法是一个 绝对导入,它只会在你的 sys.path 中查找 local_settings 模块,而不会在你的 settings.py 模块所在的同一目录下查找。你需要使用明确的相对导入:from .local_settings import *;这种写法在 Python 2.7 中也可以使用。

参考链接: PEP 328

撰写回答