65

我无法让 wsgi 为我的项目“mofin”导入我的设置文件。

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)可以正常工作。

使用 python manage.py (runserver|shell|syncdb|test store) 可以很好地加载 settings.py 文件,应用程序也是如此。

这是我的 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 设置。

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Dan xxxx', 'xxxx@yyyyyyyyyy.com'),
)

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'
)
4

15 回答 15

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 和可能的其他版本。

于 2011-08-04T23:40:02.360 回答
22

我有类似的权限问题,虽然我的 settings.py 有正确的权限,但 .pyc 没有!!!所以要注意这一点。

于 2010-11-15T02:59:13.900 回答
18

嘿,只是为这个问题添加了一个额外的答案。我有完全相同的问题,但不是文件权限。我在附加“path/to/project”,但没有附加“path/to”。链接的是 mod_wsgi 的Django 集成解释,它向我展示了答案。

于 2011-01-20T18:04:09.687 回答
8

我找到了答案……文件权限。/home/django 设置为 700。即只有 django 可以查看内容。apache 作为 Apache 运行,因此无法通过 /home/django。

于 2009-09-17T11:51:54.637 回答
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()
于 2011-09-08T11:02:35.583 回答
6

此问题的另一个原因是您无法将应用程序命名为与另一个 python 模块相同的名称。例如我打电话给 mine site,几乎没有意识到这site已经是一个 python 模块。

您可以通过启动 python 并运行import site,来检查这一点help(site),它会告诉您它没有使用您的模块。当 django 尝试导入site.settings不存在的内容时,这当然会给您错误。

于 2012-08-29T11:06:32.460 回答
4

可能的问题:

您忘记了 __init__.py 文件,该文件必须在您的项目中以及您认为要导入 python 模块的所有目录中。

您可以尝试的其他方法是将路径直接添加到 manage.py 文件中,例如:

import sys

...
...

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

我希望它有帮助

于 2009-09-11T15:10:14.307 回答
3

我有同样的问题,但另一种解决方案:

我的项目文件夹被命名为我的应用程序之一。

我有:

/home/myApp
/home/myApp/settings.py
/home/myApp/manage.py
/home/myApp/rights.py
/home/myApp/static/
/home/myApp/static/
/home/myApp/ myApp /model .py
/home/myApp/ myApp /admin.py
/home/myApp/ myApp /views.py

这种树似乎并不容易。我更改了我的项目根文件夹的名称,问题就解决了!

于 2011-08-31T08:38:38.140 回答
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,但没有这些行,并使用了该文件,现在一切正常。

于 2014-09-30T06:04:48.847 回答
1

就我而言,我有一个导致此错误的循环导入。settings.py我在另一个模块中导入一个函数,从那个模块我正在导入一个设置变量。为了修复它,我没有直接从设置中导入,而是这样做了:

from django.conf import settings
于 2013-07-04T10:15:56.763 回答
1

(我在 Apache/mod_wsgi 中为 Django 部署问题写了同样的答案。ImportError: Could not import settings 'site.settings'以防有人只发现这个问题。)

在您的情况下,这似乎不是问题,但是当我使用 WSGIPythonPath 指令(而不是 .wsgi 文件)来设置时,我遇到了相同的 ImportError sys.path。在我切换到在守护进程模式下运行 WSGI 之前,这一直很好。一旦你这样做了,你必须改用python-pathWSGIDaemonProcess 指令的参数。

于 2012-01-04T05:15:31.710 回答
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

于 2013-11-15T01:19:16.943 回答
0

乍一看,我会说 python 路径是错误的,但与交互式 shell 相比,它看起来还不错。所以也许试试这个:

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

setup_environ(settings)
于 2009-09-11T18:51:27.613 回答
0

我要说的是,您可以将项目目录插入/追加到 wsgi 文件中的 sys.path 中,但如果您的设置文件位于

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

那么你应该在那里很好。

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

这几乎总结了你正在寻找的东西。

有趣的是错误会传播:

for middleware_path in settings.MIDDLEWARE_CLASSES:

但你有什么似乎是确切的默认值

您可能想检查 wsgi 指向哪个 python 解释器。您是否打算使用 virtualenv 但 wsgi 正在查看您的系统安装?

您还可以设置运行 wsgi 的用户和组。我使用类似的东西:

WSGIDaemonProcess mysite.com 用户=skyl 组=skyl 进程=n 线程=N python-path=/home/skyl/pinax/pinax-env2/lib/python2.6/site-packages

于 2009-09-13T17:20:53.727 回答
0

我刚刚遇到这个错误,解决方案是通过 myvenv/source/activate 启用我的虚拟环境。

于 2015-07-19T11:59:53.723 回答