2012-03-13 40 views
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" 

我這樣做,我仍然得到DJANGO_SETTINGS_MODULE未定義?

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殼

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

filesystem image

+0

您是使用'django-admin.py startproject'命令設置了您的項目還是手動設置了您的項目? – bennylope 2012-03-13 22:47:45

+0

@bennylope我已經運行了大約1年,所以我不記得了。但我沒有看到爲什麼不通過指揮的理由。哦。順便說就是Pinax。所以應該是通過指揮。 – User007 2012-03-13 22:50:19

回答

4

一下添加到頂部,它看起來像路徑設置文件是不是在你的系統路徑:

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

非常感謝。我也嘗試過。再次將這段代碼添加到我的Python腳本中,我仍然得到相同的錯誤。我輸入python shell,並發現sys.path沒有我添加的新路徑。 這是一個虛擬環境,如果它很重要的話。我運行腳本時激活了它。 – User007 2012-03-14 02:09:45

+0

在你的main.py中,你能輸入下面的異常並查看回溯顯示的內容嗎?環境變量是否被適當設置? 'raise Exception(「DJANGO_SETTINGS_MODULE =」+ str(os.environ [「DJANGO_SETTINGS_MODULE」]))'如果它說:'DJANGO_SETTINGS_MODULE = None',環境變量沒有設置,我們必須驗證'os.environ [ 「DJANGO_SETTINGS_MODULE」] = ...'是在正確的地方。如果它說:'DJANGO_SETTINGS_MODULE = u「mysite.settings」',那麼我們將不得不進一步調查爲什麼它認爲沒有設置環境變量。 – Furbeenator 2012-03-14 15:56:51

+0

非常感謝。它沒有提出我們的手動例外。我包含了安裝腳本,它說'webclient.settings.'我也進入了shell來導入'webclient.settings',它就在那裏。 – User007 2012-03-14 18:11:36

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