2009-09-11 36 views
63

我無法讓wsgi爲我的項目'mofin'導入我的設置文件。如何停止導入錯誤:使用django與wsgi時無法導入設置'mofin.settings'?

從Apache錯誤日誌錯誤列表如下

mod_wsgi (pid=4001): Exception occurred within WSGI script '/var/www/wsgi-scripts/django.wsgi'. 
Traceback (most recent call last): 
    File "/usr/lib/python2.5/site-packages/django/core/handlers/wsgi.py", line 228, in __call__ 
    self.load_middleware() 
    File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py", line 31, in load_middleware 
    for middleware_path in settings.MIDDLEWARE_CLASSES: 
    File "/usr/lib/python2.5/site-packages/django/conf/__init__.py", line 28, in __getattr__ 
    self._import_settings() 
    File "/usr/lib/python2.5/site-packages/django/conf/__init__.py", line 59, in _import_settings 
    self._target = Settings(settings_module) 
    File "/usr/lib/python2.5/site-packages/django/conf/__init__.py", line 94, in __init__ 
    raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e) 
ImportError: Could not import settings 'mofin.settings' (Is it on sys.path? Does it have syntax errors?): No module named mofin.settings 

我得到了「世界,你好!」這裏列出的wsgi應用程序(http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide)能正常工作。

settings.py文件與python manage.py(runserver | shell | syncdb | test store) 一樣可以正常加載,與應用程序一樣。

這裏是我的WSGI文件:

import os 
import sys 
sys.path.append('/home/django/mofin/trunk') 
sys.path.append('/home/django/mofin/trunk/mofin') 
print >> sys.stderr, sys.path 
os.environ['DJANGO_SETTINGS_MODULE'] = 'mofin.settings' 

import django.core.handlers.wsgi 
application = django.core.handlers.wsgi.WSGIHandler() 

印在錯誤日誌中的sys.path是

['/usr/lib/python25.zip', '/usr/lib/python2.5', '/usr/lib/python2.5/plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib-dynload', '/usr/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages/gtk-2.0', '/home/django/mofin/trunk', '/home/django/mofin/trunk/mofin']

如果我打開與manage.py一個交互的shell,是的sys.path

['/home/django/mofin/trunk/mofin', '/usr/lib/python25.zip', '/usr/lib/python2.5', '/usr/lib/python2.5/plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib-dynload', '/usr/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages/gtk-2.0']

我的Django的設置文件看起來是這樣的: #爲mofin親Django的設置JECT。

DEBUG = True 
TEMPLATE_DEBUG = DEBUG 

ADMINS = (
    # ('Dan xxxx', '[email protected]'), 
) 

MANAGERS = ADMINS 

DATABASE_ENGINE = 'mysql'   # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 
DATABASE_NAME = 'mofin'    # Or path to database file if using sqlite3. 
DATABASE_USER = 'aaaaaa'    # Not used with sqlite3. 
DATABASE_PASSWORD = 'bbbbbb'   # Not used with sqlite3. 
DATABASE_HOST = ''    # Set to empty string for localhost. Not used with sqlite3. 
DATABASE_PORT = ''    # Set to empty string for default. Not used with sqlite3. 

# Local time zone for this installation. Choices can be found here: 
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name 
# although not all choices may be available on all operating systems. 
# If running in a Windows environment this must be set to the same as your 
# system time zone. 
TIME_ZONE = 'Europe/London' 

# Language code for this installation. All choices can be found here: 
# http://www.i18nguy.com/unicode/language-identifiers.html 
LANGUAGE_CODE = 'en-GB' 

SITE_ID = 1 

# If you set this to False, Django will make some optimizations so as not 
# to load the internationalization machinery. 
USE_I18N = True 

# Absolute path to the directory that holds media. 
# Example: "/home/media/media.lawrence.com/" 
MEDIA_ROOT = '/home/django/media/' 

# URL that handles the media served from MEDIA_ROOT. Make sure to use a 
# trailing slash if there is a path component (optional in other cases). 
# Examples: "http://media.lawrence.com", "http://example.com/media/" 
MEDIA_URL = 'http://mofin.mywebsite.co.uk/media/' 

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a 
# trailing slash. 
# Examples: "http://foo.com/media/", "/media/". 
ADMIN_MEDIA_PREFIX = '/admin_media/' 

# Make this unique, and don't share it with anybody. 
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' 

# List of callables that know how to import templates from various sources. 
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.load_template_source', 
    'django.template.loaders.app_directories.load_template_source', 
#  'django.template.loaders.eggs.load_template_source', 
) 

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
) 

ROOT_URLCONF = 'mofin.urls' 

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
) 

INSTALLED_APPS = (
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.admin', 
    'mofin.store' 
) 
+2

從這個問題檢查了答案: http://stackoverflow.com/questions/4194243/django-wgsi-paths – radztech 2012-03-23 02:42:18

回答

4

可能的問題:

你忘了__init__.py文件,它必須是在你的項目,並在您考慮導入一個Python模塊的所有目錄。

你可以嘗試其他的事情是直接添加路徑到manage.py文件,如:

import sys 

... 
... 

sys.path.insert(0, '/home/django/mofin/trunk') 

我希望它能幫助

+0

感謝Xidobix,我確實發現沒有__init__.py文件的另一個模塊,固定該固定runserver上的管理網站,但這仍然不起作用。直接添加路徑也沒有修復它。我想我會創建一個非常簡單的項目,並試着讓它繼續工作,然後繼續前進。 – Dan 2009-09-11 15:49:18

+0

我更加簡化了它,創建了一個新項目 「django-admin.py startproject花生」 修改了wsgi文件以指向「peanut.settings」,而不是 花生與runserver很好地工作,但與wsgi有相同的錯誤。這可能與文件權限,文件所有權或runserver和wsgi之間不同的其他一些環境變量有關嗎? – Dan 2009-09-11 16:07:44

+0

丹,我真誠地從未使用過wsgi,我使用mod_python。但是,在閱讀本指南的最後,是指向http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango的鏈接。必須有你正在尋找的答案。 祝你好運! – Xidobix 2009-09-11 16:31:02

0

在第一次看我想說的蟒蛇路徑錯誤,但與交互式外殼相比,它看起來不錯。 因此,也許試試這個:

from django.core.management import setup_environ 
from mofin import settings 

setup_environ(settings) 
0

我會說,你可以插入/追加項目目錄到sys.path中在你的WSGI文件,但如果你的設置文件是在

/home/django/mofin/trunk/mofin/settings.py 

那你應該在那裏很好。

Is it on sys.path? Does it have syntax errors? 

這幾乎總結了你在找什麼。

有趣的是,雖然錯誤傳播:

for middleware_path in settings.MIDDLEWARE_CLASSES: 

,但你有什麼似乎是exact default

您可能想要檢查wsgi指向哪個python解釋器。你打算使用virtualenv,但是wsgi正在查看你的系統安裝嗎?

您還可以設置wsgi正在運行的用戶和組。我使用類似於:

WSGIDaemonProcess mysite.com user = skyl group = skyl processes = n threads = N python-path =/home/skyl/pinax/pinax-env2/lib/python2.6/site-packages

8

我找到了答案...文件權限。/home/django設置爲700.即只有django可以查看內容。 Apache運行Apache,所以不能通過/ home/django。

+7

給予該目錄讓Apache可以讀取它的最安全權限是什麼? – 2011-12-20 16:41:26

21

我有一個類似的權限問題,雖然我的settings.py具有正確的權限,但.pyc沒有!所以小心這個。

+6

+1剛剛與OP有類似的問題,並刪除* .pyc解決它,所以謝謝。這似乎很好地工作'alias rmpyc =「find。-name」* .pyc「-exec rm -rf {} \;」''清理'一個項目 – 2011-12-31 02:40:17

+0

+1當我恢復我的Mac時,發生了! – 2012-08-15 14:12:35

18

嘿,只是爲這個問題增加一個額外的答案。我有完全相同的問題,但它不是文件權限。我正在追加「路徑/到/項目」,但不追加「路徑/到」。鏈接是mod_wsgi的Django integration explanation向我展示了答案。

+4

'進口OS,SYS 鹼= os.path.dirname(os.path.dirname(__ FILE__)) base_parent = os.path.dirname(鹼) sys.path.append(基峯) sys.path中.append(base_parent)'在任何其他py語句之前。解決了這個問題。感謝提示:-) – nemesisfixx 2012-04-30 10:49:51

48

如果您的應用程序(包含init文件的項目的子目錄)與項目名稱相同,也會發生這種情況。您的settings.py文件可能位於您的項目文件夾中,但似乎django系統的一部分似乎首先爲項目內的某個模塊尋找與該項目相同的名稱,以及何時在該處找不到settings.py ,它帶有誤導性的消息。

-uniquename1 

---settings.py 

---manage.py 

---application1 

-----file.py 

-----file2.py 

---uniquename1 (problem, rename this to some other unique name) 

-----file.py 

-----file2.py 

只是別的東西來檢查其他人有這個問題。適用於Django 1.3以及其他人。

+3

是否有其他解決方案,然後重命名dirs? – 2014-04-27 21:03:51

+0

我當時沒有找到解決方案,但是如果這仍然發生,我想這不是開發人員優先考慮的問題。挖掘這個原因超出了我的技能。我相信它是在必須做出改變的核心繫統中。 – shanemgrey 2015-01-20 16:26:18

+0

我不認爲你是對的Shaneveeg。在我的情況下,在執行「django-admin.py startproject mysite」時,「settings.py」位於項目文件夾的「第二級別」中:https://docs.djangoproject.com/en/1.6/ intro/tutorial01/ – Timo 2015-04-12 09:11:56

3

我有同樣的問題,但另一種解決方案:

我的項目文件夾命名正是因爲我的應用程序之一。

我:

/家庭/對myApp
/home/myApp/settings.py
/home/myApp/manage.py
/home/myApp/rights.py
/家/對myApp /靜態/
/家庭/對myApp /靜態/
/家庭/對myApp/對myApp /model.py
/家庭/對myApp/對myApp /admin.py
/家庭/對myApp/對myApp /views.py

這種樹的似乎並不可能輕易。 我改變了我的項目根文件夾的名稱,問題解決了!

+1

我相信現在可以(1.4),因爲我沒有看到這個問題。 https://code.djangoproject.com/ticket/1908 – 2013-02-18 02:46:39

+1

只有在項目根文件夾中有__init__.py文件時纔會出現此問題,因爲它會導致導入器錯誤地認爲這是可以導入的模塊(取而代之看它裏面的同名模塊) – tato 2016-02-29 11:05:58

7

我想你需要一個尾隨的正斜槓,因爲它在我加載django之前必須在我的apache的wsgi腳本中完成。

import os 
import sys 
sys.path.append('/home/django/mofin/trunk/') 
sys.path.append('/home/django/mofin/trunk/mofin/') 
print >> sys.stderr, sys.path 
os.environ['DJANGO_SETTINGS_MODULE'] = 'mofin.settings' 

import django.core.handlers.wsgi 
application = django.core.handlers.wsgi.WSGIHandler() 

在我的情況

import os 
import sys 
if os.uname()[1] == 'vivien': 
    sys.path.append('/home/www/sitebuilder.blacknight.ie/web/') 
    os.environ['DJANGO_SETTINGS_MODULE'] = 'gibo.dev_settings' 
elif os.uname()[1] == 'thingy': 
    sys.path.append('/home/www/sitebuilder.blacknight.ie/web/') 
    os.environ['DJANGO_SETTINGS_MODULE'] = 'gibo.dev_settings' 
else: 
    sys.path.append('/home/www/sitebuilder.blacknight.ie/web/') 
    os.environ['DJANGO_SETTINGS_MODULE'] = 'gibo.settings' 

import django.core.handlers.wsgi 
application = django.core.handlers.wsgi.WSGIHandler() 
+0

這是我的問題。添加第二個附加語句修復了它!謝謝您的幫助! – 2012-02-02 03:16:16

+0

有人應該upvote這個答案! **添加到PYTHONPATH時需要使用尾部斜線**。荒謬!它吃了一個小時的時間。 – 2013-03-07 15:13:07

1

(我的情況下,有人寫了這個相同的答案Django deployment problem in Apache/mod_wsgi. ImportError: Could not import settings 'site.settings'只發現了這個問題。)

這似乎並非是在你的問題的情況,但是當我使用WSGIPythonPath指令(而不是.wsgi文件)設置sys.path時,我碰到了相同的ImportError。這工作得很好,直到我切換到在守護進程模式下運行WSGI。一旦你這樣做了,你必須改用WSGIDaemonProcess指令的python-path參數。

6

這個問題的另一個原因是你不能命名你的應用程序與另一個python模塊相同。例如,我打電話給我的site,很少意識到site已經是一個python模塊。

你可以通過啓動python來檢查,並運行import sitehelp(site),它會告訴你它沒有使用你的模塊。當django試圖導入site.settings時,這當然會給你帶來錯誤。

0

添加的代碼

sys.path.append('/home/django/mofin/mofin') 
sys.path.append('/home/django/mofin/trunk/mofin/mofin') 

兩行到你的WSGI文件線下:

sys.path.append('/home/django/mofin/trunk/mofin/') 
1

對我來說,我有一個圓形進口造成這個錯誤。從settings.py我在另一個模塊中導入一個函數,並從該模塊導入一個設置變量。爲了解決這個問題,而不是從設置直接進口,我這樣做:

from django.conf import settings 
0

我也有類似的問題,在我的Python下面的代碼片段解決了這個問題:

ALLDIRS = ['/var/www/MarkerDB/'] 

import sys 
import site 

# Remember original sys.path. 
prev_sys_path = list(sys.path) 

# Add each new site-packages directory. 
for directory in ALLDIRS: 
    site.addsitedir(directory) 

# Reorder sys.path so new directories at the front. 
new_sys_path = [] 
for item in list(sys.path): 
    if item not in prev_sys_path: 
     new_sys_path.append(item) 
     sys.path.remove(item) 
sys.path[:0] = new_sys_pat 

來源:http://code.google.com/p/modwsgi/wiki/VirtualEnvironments#Application_Environments

1

讓我來補充一下我的經驗。撞頭了幾個小時,從上面的答案想盡一切後,我發現,在settings.py文件的幾行導致此問題:

from south.modelsinspector import add_introspection_rules 
add_introspection_rules([], ["^dynamicsites.fields.FolderNameField"]) 
add_introspection_rules([], ["^dynamicsites.fields.SubdomainListField"]) 

之後,我做了settings.py的副本,名爲scripts_settings.py內部消除該線,並使用該文件,現在一切正常。

0

我只是有這個錯誤,解決辦法是通過myvenv/source/activate啓用我的虛擬環境。

相關問題