我在使用python 2.7在可扩展的OpenShift平台上运行Django(1.8.7)应用程序时遇到问题。由于某种原因,它无法获得环境变量OPENSHIFT_POSTGRESQL_DB_URL
的信息。这是错误:
mod_wsgi (pid=69306): Target WSGI script '/var/lib/openshift/5d5/app-root/runtime/repo/wsgi/application' cannot be loaded as Python module.
mod_wsgi (pid=69306): Exception occurred processing WSGI script '/var/lib/openshift/5d5/app-root/runtime/repo/wsgi/application'.
Traceback (most recent call last):
File "/var/lib/openshift/5d5/app-root/runtime/repo/wsgi/application", line 21, in <module>
application = get_wsgi_application()
File "/var/lib/openshift/5d5/python/virtenv/lib/python2.7/site-packages/Django-1.8.7-py2.7.egg/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "/var/lib/openshift/5d5/python/virtenv/lib/python2.7/site-packages/Django-1.8.7-py2.7.egg/django/__init__.py", line 17, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/var/lib/openshift/5d5/python/virtenv/lib/python2.7/site-packages/Django-1.8.7-py2.7.egg/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/var/lib/openshift/5d5/python/virtenv/lib/python2.7/site-packages/Django-1.8.7-py2.7.egg/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/var/lib/openshift/5d5/python/virtenv/lib/python2.7/site-packages/Django-1.8.7-py2.7.egg/django/conf/__init__.py", line 92, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/opt/rh/python27/root/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/var/lib/openshift/5d5/app-root/runtime/repo/wsgi/bet/controller/settings.py", line 59, in <module>
url = urlparse.urlparse(os.environ.get('OPENSHIFT_POSTGRESQL_DB_URL'))
File "/opt/rh/python27/root/usr/lib64/python2.7/urlparse.py", line 143, in urlparse
tuple = urlsplit(url, scheme, allow_fragments)
File "/opt/rh/python27/root/usr/lib64/python2.7/urlparse.py", line 182, in urlsplit
i = url.find(':')
AttributeError: 'NoneType' object has no attribute 'find'
这就是我在之前使用过的settings.py中使用该变量的方法:
import urlparse
DATABASES = {}
if ON_OPENSHIFT:
url = urlparse.urlparse(os.environ.get('OPENSHIFT_POSTGRESQL_DB_URL'))
DATABASES['default'] = {
'ENGINE' : 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['OPENSHIFT_APP_NAME'],
'USER': url.username,
'PASSWORD': url.password,
'HOST': url.hostname,
'PORT': url.port,
}
这是我的application
文件:
#!/usr/bin/env python
import os
import sys
sys.path.append(os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi', 'bet'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'controller.settings'
virtenv = os.environ['OPENSHIFT_PYTHON_DIR'] + '/virtenv/'
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
execfile(virtualenv, dict(__file__=virtualenv))
except:
pass
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
如果我通过rhc ssh bet
登录我的应用并尝试打印变量,则可以正常运行:
echo $OPENSHIFT_POSTGRESQL_DB_URL
postgresql://admin:password-mynick.rhcloud.com:port/
想一想我错过了什么。我看起来here没有运气。
注意:我的应用名为bet
。但是,由于我将其转移到可扩展的应用程序,我在openshift中创建了abet
应用程序。我在bet
文件夹中将应用名称保留为wsgi
。这可能导致一些问题吗?