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 ) が正常に動作するようにします。

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 でインタラクティブ シェルを開くと、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

この問題を抱えている他の人をチェックするための何か他のもの。Django1.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

この問題のもう 1 つの原因は、アプリケーションに別の Python モジュールと同じ名前を付けられないことです。たとえば、私は mine を呼び出しましたが、それが既に python モジュールsiteであることをほとんど認識していません。site

これは、python を起動して , を実行することで確認できimport siteますhelp(site)。モジュールが使用されていないことが示されます。site.settingsもちろん、djangoが存在しないものをインポートしようとすると、エラーが発生します。

于 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

私は同じ問題を抱えていましたが、別の解決策:

私のプロジェクト フォルダーは、私のアプリケーションの 1 つとまったく同じ名前でした。

私が持っていた:

/home/myApp
/home/myApp/settings.py
/home/myApp/manage.py
/home/myApp/rights.py
/home/myApp/static/
/home/myApp/static/
/home/myApp/ myApp /モデル.py
/home/myApp/ myApp /admin.py
/home/myApp/ myApp /views.py

このような木はなかなか出来そうにありません。プロジェクトのルート フォルダの名前を変更したところ、問題は解決しました。

于 2011-08-31T08:38:38.140 に答える
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' in case someone only found this question.)

これはあなたのケースでは問題ではないようですが、WSGIPythonPath ディレクティブ (.wsgi ファイルの代わりに) を使用して .wsgi をセットアップしたときに、同じ ImportError に遭遇しましたsys.path。WSGI をデーモン モードで実行するように切り替えるまでは、問題なく動作していました。これを行ったら、python-path代わりに WSGIDaemonProcess ディレクティブへの引数を使用する必要があります。

于 2012-01-04T05:15:31.710 に答える
0

このエラーが発生したばかりで、解決策は myvenv/source/activate を介して仮想環境を有効にすることでした。

于 2015-07-19T11:59:53.723 に答える
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:

but you have what appears to be the exact default.

You might want to check which python interpreter is pointed to by wsgi. Are you intending to use a virtualenv but wsgi is looking at your system install?

You can also set the user and group that wsgi is running under. I use something like:

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

于 2009-09-13T17:20:53.727 に答える
0

一見したところ、Python パスが間違っていると思いますが、インタラクティブ シェルと比較すると問題ないように見えます。だから多分これを試してください:

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

setup_environ(settings)
于 2009-09-11T18:51:27.613 に答える