ImportError:无法导入设置

时间:2015-12-04 16:43:52

标签: django python-2.7

在Windows 8上,我在Django(1.6.5)中创建了一个示例项目,当我运行我编写的自定义命令(runtcpserver)时,我遇到了错误。

这就是我的项目结构:

C:/ django的/ entitetracker:

manage.py

tcpserver/
    forms.py
    views.py
    models.py
    urls.py
    management
       __init__.py
       command
           __init__.py
           runtcpserver.py

settings/
    __init__.py
    base.py
    local.py

我的manage.py文件如下:

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

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

    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)

我的路径(python 2.7):

>>> import sys
>>> for path in sys.path: print path
C:\django\entitetracker
(...other paths)

当我运行python manage.py runtcpserver settings = settings.local命令时,我收到以下错误:

Traceback (most recent call last):
File "manage.py", line 9, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 261, in fetch_command
commands = get_commands()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 107, in get_commands
apps = settings.INSTALLED_APPS
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
self._setup(name)
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 49, in _setup
self._wrapped = Settings(settings_module)
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 132, in __init__
% (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'entitetracker.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named settings

在python shell中,我尝试导入设置模块,但我没有收到任何错误:

>>> from settings import local
>>> 

有人可以建议我缺少什么吗?

2 个答案:

答案 0 :(得分:2)

您的PYTHONPATHC:\django\entitetracker。您可以加载entitetracker.settings。完成后,Python尝试查找C:\django\entitetracker\entitetracker\settings包。

使用

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

答案 1 :(得分:0)

天哪,我真傻。错误是我在settings = settings.local之前错过了两个破折号!感谢你帮助Tomasz。