ImportError:无法导入设置

时间:2012-10-20 10:48:06

标签: python django

我正在尝试在Django中开发一个示例项目,并在运行syncdb命令时出错。

这就是我的项目结构:

/用户/ django_demo / godjango /预定:

manage.py
    registration/
        forms.py
        views.py
        models.py
        urls.py
    bookings/
        settings.py
        urls.py

我的manage.py文件如下:

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

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bookings.settings")
    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)

我的PYTHONPATH和DJANGO_SETTINGS_MODULE设置如下

$echo $DJANGO_SETTINGS_MODULE
bookings.settings

$ echo $PYTHONPATH
/Users/django_demo/godjango/bookings

我的WSGI.py文件如下所示:

 import os
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bookings.settings")
 from django.core.wsgi import get_wsgi_application
 application = get_wsgi_application()

当我运行python manage.py syncdb命令时,我收到以下错误。

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 69, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 8, in <module>
    from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/core/management/sql.py", line 6, in <module>
    from django.db import models
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module>
    if DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
    self._setup()
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/django_demo/godjango/lib/python2.7/site-packages/django/conf/__init__.py", line 95, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'bookings.settings' (Is it on sys.path?): No module named unipath

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

6 个答案:

答案 0 :(得分:33)

错误说ImportError: Could not import settings 'bookings.settings' (Is it on sys.path?): No module named unipath

那么,你的路径是/Users/django_demo/godjango/bookings在python-sys.path吗?

使用以下命令在shell中查看:

$ python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for path in sys.path: print path
... 

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/suds-0.4-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL-1.1.7-py2.7-macosx-10.6-intel.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/spyne-2.8.2_rc-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-3.0alpha2-py2.7-macosx-10.6-intel.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pytz-2012d-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_debug_toolbar-0.9.4-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django_social_auth-0.7.7-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/python_openid-2.2.5-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/oauth2-1.5.211-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.4.1-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/httplib2-0.7.6-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
/Library/Python/2.7/site-packages
>>> 

如果没有 - 您需要添加它或只是将bookings应用移动到sys.path

中表示的路径之一

答案 1 :(得分:14)

修改您的wsgi.py文件
 import os
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bookings.settings")
 from django.core.wsgi import get_wsgi_application
 application = get_wsgi_application()

import os, sys    
sys.path.append(' /Users/Sreek/django_demo/godjango/bookings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bookings.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

答案 2 :(得分:11)

这里追溯的重要部分就在最后。它说“没有名为unipath的模块”。你已经在代码中的某个地方提到了它,但你似乎没有在你的项目中使用它 - 它不是标准库的一部分,因此你需要将它安装在Python可以看到的地方。

答案 3 :(得分:4)

或者,您甚至可以在运行时传递设置路径,如下所示:

python manage.py syncdb --settings=bookings.settings --pythonpath=/Users/django_demo/godjango/bookings

这应该覆盖环境变量(DJANGO_SETTINGS_MODULE)

答案 4 :(得分:1)

查看小时/etc/apache2/httpd.conf文件;您必须包含WSGIPythonPath指令,以指示包含Django项目的文件夹(manage.py文件),如:

WSGIPythonPath / home / user / Projects / Django / MyProject

如果您在VirtualHost中使用了一些奇怪的端口,请指定是否要收听:

听90

希望这有助于某人

答案 5 :(得分:1)

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

在我的情况下,我使用__init__.py导入其他文件并且其中存在错误,因此设置可能实际存在但存在缺陷。