gpt4 book ai didi

DJANGO_SETTINGS_MODULE 未定义?

转载 作者:行者123 更新时间:2023-12-05 00:54:10 32 4
gpt4 key购买 nike

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 功能的小脚本,但它告诉我环境变量问题。

我能做什么?我整个下午都在阅读许多帖子....

感谢您的帮助!这是我第一次击中它。


回溯。它不会引发我们的手动异常。它给出了与上面相同的异常。

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 shell

>>> 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/deploy/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()

filesystem image

最佳答案

将此添加到顶部,看起来您的设置文件的路径不在您的系统路径中:

path = '/path/to/mysite'
if path not in sys.path:
sys.path.append(path)

关于DJANGO_SETTINGS_MODULE 未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9693368/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com