3
import os
from os.path import abspath, dirname
import sys

# Set up django
project_dir = abspath(dirname(dirname(__file__)))
sys.path.insert(0, project_dir)
os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"

私はそれをしました、そして私はまだ得ます

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    from django.middleware.csrf import get_token
  File "/var/lib/system-webclient/system-webenv/lib/python2.6/site-packages/django/middleware/csrf.py", line 14, in <module>
    from django.utils.cache import patch_vary_headers
  File "/var/lib/system-webclient/system-webenv/lib/python2.6/site-packages/django/utils/cache.py", line 24, in <module>
    from django.core.cache import cache
  File "/var/lib/system-webclient/system-webenv/lib/python2.6/site-packages/django/core/cache/__init__.py", line 68, in <module>
    cache = get_cache(settings.CACHE_BACKEND)
  File "/var/lib/system-webclient/system-webenv/lib/python2.6/site-packages/django/utils/functional.py", line 276, in __getattr__
    self._setup()
  File "/var/lib/system-webclient/system-webenv/lib/python2.6/site-packages/django/conf/__init__.py", line 38, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined

これはいくつかの django 機能を呼び出す小さなスクリプトでしたが、ENVIRONMENT VARIABLE の問題を教えてくれます。

私に何ができる?私は午後中ずっと多くの投稿を読んで過ごしました....

助けてくれてありがとう!打ったのはこれが初めてでした。


トレースバック。手動例外を発生させません。上記と同じ例外を与えています。

webclient/main.py

import os
from os.path import abspath, dirname
import sys
path = '/var/lib/graphyte-webclient/webclient'
if path not in sys.path:
    sys.path.append(path)
sys.path.insert(0, path)
os.environ["DJANGO_SETTINGS_MODULE"] = "webclient.settings"
raise Exception("DJANGO_SETTINGS_MODULE = " + str(os.environ["DJANGO_SETTINGS_MODULE"]))

    .... other things

manage.pyシェル内

>>> from webclient.settings import settings
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: cannot import name settings

>>> import webclient
>>> webclient
<module 'webclient' from '/var/lib/system-webclient/webclient/../webclient/__init__.pyc'>

>>> import webclient.settings
>>> webclient.settings
<module 'webclient.settings' from '/var/lib/system-webclient/webclient/../webclient/settings.pyc'>

webclient/deploy/pinax.fcgi

import os
import sys

from os.path import abspath, dirname, join
from site import addsitedir

sys.path.insert(0, abspath(join(dirname(__file__), "../../")))

from django.conf import settings
os.environ["DJANGO_SETTINGS_MODULE"] = "webclient.settings"

sys.path.insert(0, join(settings.PROJECT_ROOT, "apps"))

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

webclient/デプロイ/pinax.wcgi

import os
import sys

from os.path import abspath, dirname, join
from site import addsitedir

sys.path.insert(0, abspath(join(dirname(__file__), "../../")))

from django.conf import settings
os.environ["DJANGO_SETTINGS_MODULE"] = "webclient.settings"
#os.environ["SCRIPT_NAME"] = "/natrium"

sys.path.insert(0, join(settings.PROJECT_ROOT, "apps"))

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

ファイルシステム イメージ

4

2 に答える 2

4

これを先頭に追加します。設定ファイルへのパスがシステム パスにないようです。

path = '/path/to/mysite'
if path not in sys.path:
   sys.path.append(path)
于 2012-03-14T00:23:24.393 に答える
0

django 1.6 + 以下のコードを使用している場合

import os
import sys
sys.path.append('/path/to/your/project/')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
from django.contrib import auth
from django.conf import settings
from django.contrib.auth.models import User
于 2015-10-18T08:37:37.030 に答える