2012-10-20 90 views
32

我想在Django中開發一個示例項目,並在運行syncdb命令時出現錯誤。ImportError:無法導入設置

這是我的項目結構看起來像:

/用戶/ 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 

有人可以建議我失蹤了嗎?

回答

31

錯誤說ImportError: Could not import settings 'bookings.settings' (Is it on sys.path?): No module named unipath

那麼,是Python-內的sys.path你的路徑/Users/django_demo/godjango/bookings

以檢查它在你的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

+4

怎樣才能「動」我的應用程序進入路徑之一? – Timo

+2

@Timo:如果你使用virtualenv,你可以使用命令'add2virtualenv' – jnns

+0

,任何人都可以幫助我解決這個問題。我也面對這樣的錯誤。 http://stackoverflow.com/questions/33890277/deploy-django-on-apache?noredirect=1#comment55585809_33890277仍然無法解決。 – uma

11

的的顯著部分所代表的路徑之一這裏的追溯就在最後。它說「沒有名爲unipath的模塊」。您已經在代碼中的某個地方提到過,但您的項目似乎沒有它 - 它不是標準庫的一部分,所以您需要將它安裝在Python可以看到的地方。

14

修改從

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

您wsgi.py文件

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() 
+0

這是我的問題所在。 – Greg

1

審查小時/etc/apache2/httpd.conf文件;您必須包含WSGIPythonPath指令,以指示包含您的Django項目的文件夾(管理。PY文件),如:

WSGIPythonPath /家庭/用戶/項目/ Django的/ MyProject的

,如果你還用你的虛擬主機一些奇怪的端口中,指定用於監聽:

聽90

希望這有助於有人

+0

如果它在PYTHONPATH中,那麼我認爲這不是必要的,是嗎? – User

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到導入其他文件並出現錯誤,因此設置可能實際存在但存在缺陷。

4

或者,你甚至可以在運行時通過設置路徑,像這樣:

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

這應該覆蓋環境變量(DJANGO_SETTINGS_MODULE)

+0

Aww耶夥計!你節省了我的電力(2,64)小時!謝謝 ! – dctremblay

相關問題