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功能的小脚本,但它告诉我环境变量问题。
我该怎么办?我花了整个下午阅读了很多帖子......
感谢您的帮助!这是我第一次打它。
追溯。它不会引起我们的手动异常。它给出了与上述相同的例外。
Web客户端/ 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'>
Web客户端/部署/ 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")
Web客户端/部署/ 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()
答案 0 :(得分:4)
将其添加到顶部,看起来设置文件的路径不在您的系统路径中:
path = '/path/to/mysite'
if path not in sys.path:
sys.path.append(path)
答案 1 :(得分: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