运行服务器设置时出现Django错误

时间:2017-01-03 15:45:15

标签: python django python-2.7 import settings

我试图执行

python manage.py runserver

但它引发了一个错误说

ImportError: Could not import settings 'survivalguide.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named survivalguide.settings

我使用python 2.7安装了Anaconda并设置了virtualenv。

我的manage.py

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "survivalguide.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)

我当前的os.path

[
    '',
    '/Users/KWYI/Documents/Python_Practice/Django_Practice/survivalguide',
    '/Users/KWYI/anaconda/envs/PyCon/lib/python27.zip',
    '/Users/KWYI/anaconda/envs/PyCon/lib/python2.7',
    '/Users/KWYI/anaconda/envs/PyCon/lib/python2.7/plat-darwin',
    '/Users/KWYI/anaconda/envs/PyCon/lib/python2.7/plat-mac',
    '/Users/KWYI/anaconda/envs/PyCon/lib/python2.7/plat-mac/lib-scriptpackages',
    '/Users/KWYI/anaconda/envs/PyCon/lib/python2.7/lib-tk',
    '/Users/KWYI/anaconda/envs/PyCon/lib/python2.7/lib-old',
    '/Users/KWYI/anaconda/envs/PyCon/lib/python2.7/lib-dynload',
    '/Users/KWYI/anaconda/envs/PyCon/lib/python2.7/site-packages',
    '/Users/KWYI/anaconda/envs/PyCon/lib/python2.7/site-packages/setuptools-27.2.0-py2.7.egg'
]

我现在排除故障几个小时,但仍然无法弄清问题是什么。

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

DJANGO_SETTINGS_MODULE设置为'survivalguide.settings'通常有效,因为manage.py位于settings.py上方的目录中。

manage.pysettings.py位于同一目录中,这是不常见的。我建议将manage.py移到父目录中。除非你明白自己在做什么,否则我会避免使用自定义项目布局,否则你可能会遇到更多这样的问题。

如果您不能这样做,您可以将行更改为:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

或者您可以手动将/Users/KWYI/Documents/Python_Practice/Django_Practice/添加到python路径。