我想從python到達我的MySQL數據庫。它運行在Openshift - Python 2.7,MySQL 5.5上。 Django和Mysql部署運行得很好。 PHPMyAdming的作品。raise ImportError「設置無法導入」ENVIRONMENT_VARIABLE
編輯setup.py
setup(
name='YourAppName',
version='1.0',
description='OpenShift App',
author='Your Name',
author_email='[email protected]',
url='http://www.python.org/sigs/distutils-sig/',
install_requires=['Django<=1.4', 'MySQL-python',],
)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ['OPENSHIFT_APP_NAME'],
'USER': os.environ['OPENSHIFT_MYSQL_DB_USERNAME'],
'PASSWORD': os.environ['OPENSHIFT_MYSQL_DB_PASSWORD'],
'HOST': os.environ['OPENSHIFT_MYSQL_DB_HOST'],
'PORT': os.environ['OPENSHIFT_MYSQL_DB_PORT']
}
}
但是,當我把這個
from django.db import connection
到一些PY劇本我看到這個日誌:
mod_wsgi (pid=327977): Target WSGI script '/var/lib/openshift/542145275xxxxx9330000e6/app-root/runtime/repo/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=327977): Exception occurred processing WSGI script '/var/lib/openshift/542145275xxxxx9330000e6/app-root/runtime/repo/wsgi.py'.
Traceback (most recent call last):
File "/var/lib/openshift/542145275xxxxx9330000e6/app-root/runtime/repo/wsgi.py", line 18, in <module>
from django.db import connection
File "/var/lib/openshift/542145275xxxxx9330000e6/python/virtenv/lib/python2.7/site-packages/Django-1.4-py2.7.egg/django/db/__init__.py", line 11, in <module>
if DEFAULT_DB_ALIAS not in settings.DATABASES:
File "/var/lib/openshift/542145275xxxxx9330000e6/python/virtenv/lib/python2.7/site-packages/Django-1.4-py2.7.egg/django/utils/functional.py", line 184, in inner
self._setup()
File "/var/lib/openshift/542145275xxxxx9330000e6/python/virtenv/lib/python2.7/site-packages/Django-1.4-py2.7.egg/django/conf/__init__.py", line 40, 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.
可能是什麼原因問題? 感謝您的任何答案。
編輯: 部署,因爲它是,沒有settings.py(參照本應在DJANGO_SETTINGS_MODULE,我說得對不對?所以,現在我創建了一個後來我改變的變量是這樣的:
export DJANGO_SETTINGS_MODULE=var.lib.openshift.542145275xxxxx9330000e6.app-root.runtime.repo.settings
我嘗試了不同的值,子文件夾等林不知道什麼正確的值
我刪除從setup.py數據庫並將它們轉移到我的新創建的settings.py 它看起來像這樣:。
import os
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ['OPENSHIFT_APP_NAME'],
'USER': os.environ['OPENSHIFT_MYSQL_DB_USERNAME'],
'PASSWORD': os.environ['OPENSHIFT_MYSQL_DB_PASSWORD'],
'HOST': os.environ['OPENSHIFT_MYSQL_DB_HOST'],
'PORT': os.environ['OPENSHIFT_MYSQL_DB_PORT']
}
}
沒有變化。 爲什麼DJAGNO_SETTING_MODULE仍然是「未定義」(即使它會被定義爲錯誤)?
「設置無法導入的部分,因爲環境變量DJANGO_SETTINGS_MODULE未定義。」令你感到困惑? – geoffspear 2014-09-24 15:28:58
好的,我沒有寫得好。我能做些什麼來定義DJANGO_SETTINGS_MODULE? – 2014-09-24 15:34:05
@PařezPařez:更具體。你是如何研究這個問題的?你採取了哪些步驟? (我假設你已經問了Google如何設置一個環境變量,我還假設你已經諮詢了關於如何使用DJANGO_SETTINGS_MODULE的django文檔)。 – 2014-09-24 15:56:21