未加星标

Gahhh!! Django, virtualenv, and cx_Oracle

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
UPDATE:

Thanks in advance to the comments from Graham Dumpleton whose comments below pointed me in the right direction!

This

was is

a plea for help.

I've got django installed and configured with apache and virtualenv. I also have one particular app (named myapp ) that queries an Oracle database directly (django is configured to use mysql). All of the apps work, except for anything thatbrequires the myapp app... which includes the admin!

Requesting any view that uses cx_Oracle results in a cryptic error simiar to:

ViewDoesNotExist at /some/url/

Could not import myproject.myapp.views. Error was: libclntsh.so.10.1: cannot open shared object file: No such file or directory

The full traceback follows:

Environment:
Request Method: GET
Request URL: http://mydomain.com/myapp/foo/
Django Version: 1.1.1
python Version: 2.6.2
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.admindocs',
'django.contrib.flatpages',
'django_extensions',
'myproject.userprofile',
'myproject.myapp',
... snip...
'myproject.utils']
Installed Middleware:
('django.middleware.gzip.GZipMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware')
Traceback:
File "/home/django/.virtualenvs/myproject/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
83. request.path_info)
File "/home/django/.virtualenvs/myproject/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve
218. sub_match = pattern.resolve(new_path)
File "/home/django/.virtualenvs/myproject/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve
218. sub_match = pattern.resolve(new_path)
File "/home/django/.virtualenvs/myproject/lib/python2.6/site-packages/django/core/urlresolvers.py" in resolve
125. return self.callback, args, kwargs
File "/home/django/.virtualenvs/myproject/lib/python2.6/site-packages/django/core/urlresolvers.py" in _get_callback
134. raise ViewDoesNotExist, "Could not import %s. Error was: %s" % (mod_name, str(e))
Exception Type: ViewDoesNotExist at /myapp/foo/
Exception Value: Could not import myproject.myapp.views. Error was: libclntsh.so.10.1: cannot open shared object file: No such file or directory

The strange thing is that tests using cx_Oracle run correctly (as does code typed into the interpreter via ./manage.py shell), so cx_Oracle is installed correctly in the virtualenv. Orignally, I thought that apache was somehow misconfigured. The problem, however, is that Apache just doesn't know where to find the shared libraries for cx_Oracle's C extension modules. On my system (Ubuntu), shared libraries are located in /usr/lib and /usr/local/lib .

When building cx_Oracle, you must include an environment variable for ORACLE_HOME and update the LD_LIBRARY_PATH so that it also includes ORACLE_HOME. While logged in as the django user, I updated .bashrc so that it includes the following:

export ORACLE_HOME=/home/django/oracle/instantclient_10_2
export LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH

For me, all of the cx_Oracle shared libraries are located in /home/django/oracle/instantclient_10_2 . So, (with my virtualenv activated) I built, installed, and tested cx_Oracle, and everything worked fine, because my user environment was set up so that these shared libraries are available.

To make these available to Apache, I created hard links to all of the shared libraries from /usr/local/lib, and then ran ldconfig. Note that ldconfig ignores symbolic links!

cd /usr/local/lib
sudo ln /home/django/oracle/instantclient_10_2/libclntsh.so.10.1
# also creating links to all other libs
sudo ldconfig

After restarting Apache, my django apps worked as expected. For the curious, my wsgi script and apache config follows.

My .wsgi script

import sys
import site
import os
vepath = '/home/django/.virtualenvs/myproject/lib/python2.6/site-packages'
prev_sys_path = list(sys.path)
# add the site-packages of our virtualenv as a site dir
site.addsitedir(vepath)
# add the app's directory to the PYTHONPATH
sys.path.append('/home/django/django_projects/myproject_root/myproject/')
sys.path.append('/home/django/django_projects/myproject_root/')
os.environ['PYTHON_EGG_CACHE'] = '/home/django/.python-eggs'
new_sys_path = [p for p in sys.path if p not in prev_sys_path]
for item in new_sys_path:
sys.path.remove(item)
sys.path[:0] = new_sys_path
from django.core.handlers.wsgi import WSGIHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
application = WSGIHandler()

The Pertinent part of the Apache Config, located within a VirtualHost Directive:

Alias /media/ /var/www/media/
Alias /site_media/ /var/www/static/
# Run WSGI in Daemon Mode:
WSGIDaemonProcess myproject user=www-data group=www-data threads=25
WSGIProcessGroup myproject
WSGIScriptAlias / /home/django/django_projects/myproject_root/apache_mod_wsgi_conf/myproject.wsgi

NOTE: Graham Dumpleton suggests four options to get around the original problem (and I paraphrase):

Rebuild cx_Oracle setting the LD_RUN_PATH - I tried this, but was unnsuccessful. Put the shared libraries in /usr/lib - I hard linked to the libraries from /usr/local/lib. This is the solution that worked. Modify /etc/ld.conf so that it includes the path to the shared libraries - I considered adding a file to /etc/ld.so.conf.d/ which would have included the path to my ORACLE_HOME directory. However, the above options worked, so I went with that instead. Edit /etc/apache/envvars so that it includes the appropriate environment variables - I also tried this approach, including the same environment variables that I added to my .bashrc, but this also did not work

Hope this helps someone else!

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: DjangoMySQLUbuntuSQLGISSGRYDUPythonTI
分页:12
转载请注明
本文标题:Gahhh!! Django, virtualenv, and cx_Oracle
本站链接:http://www.codesec.net/view/479908.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(101)