设为首页 收藏本站
查看: 1963|回复: 0

[经验分享] Python cx

[复制链接]

尚未签到

发表于 2015-11-29 15:08:13 | 显示全部楼层 |阅读模式
  SQLAlchemy 是 Python 中用来操作数据库的一个利器,支持 MySQL、Oracle、PostgreSQL、SQLite、Oracle。使用 SQLAlchemy 来管理 Oracle 的数据需要安装依赖 cx_Oracle。在这过程中遇到不少问题,记录如下。
  cx_Oracle 可以到这里下载:http://cx-oracle.sourceforge.net/
  
旧版本的下载地址:http://sourceforge.net/projects/cx-oracle/files/
  现在也可以通过 pip install cx_Oracle 安装

Windows
  安装 cx_Oracle 正确版本,需要区分 10g, 11g,安装 instantclient_11_2(请到 Oracle 官网下载)

问题
  

ImportError: DLL load failed: 找不到指定的模块。  

  将 instantclient_11_2 所在的目录添加到环境变量,但是环境变量有时没有立即生效
  
,可以复制 oci.dll(版本也要正确)到 \Python27\Lib\site-packages 目录下
  这时又出现问题
  

Unable to acquire Oracle environment handle,  

  复制 oci.dll 依赖的 dll,oraociei11.dll,ocijdbc11.dll 到 \Python27\Lib\site-packages 目录下。如果不清楚就将 instantclient_11_2 目录下的 dll 都复制到 \Python27\Lib\site-packages 目录下
  可以参考:http://docs.oracle.com/cd/B12037_01/appdev.101/b10779/oci01int.htm#423364
  

# UNIX  
libclnstsh.so.10.1
  
libnnz10.so
  
libociei.so
  

  
# Windows
  
oci.dll
  
oraociei10.dll
  
orannzsbb10.dll
  

Linux
  

# 先安装 rpm  
yum install rpm
  

# oracle-instantclient-basic-10.2.0.3-1.x86_64.rpm 请到 Oracle 官网下载  
rpm -ivh oracle-instantclient-basic-10.2.0.3-1.x86_64.rpm
  
rpm -ivh cx_Oracle-5.1.2-10g-py27-1.x86_64.rpm
  
# 有这个文件表示安装成功,根据 python 的位置,也可能在其他地方
  
ls /usr/lib/python2.7/site-packages/cx_Oracle.so
  

问题 1
  

import cx_Oracle  Traceback (most recent call last):
  File "", line 1, in
  ImportError: libclntsh.so.10.1: cannot open shared object file: No such file or directory
  

  The problem is that the just installed libraries of the Oracle Instant Client aren’t part of the default library path of your CentOS installation. Either you have to extend the LD_LIBRARY_PATH Bash variable of your current user or you have to add the lib directory of the Instant Client installation to the system-wide library path if all users should be allowed to use the Oracle Instant Client. To do so you have to create a new file, e.g. oracle.conf, in the /etc/ld.so.conf.d directory with the following content:
  

vim /etc/ld.so.conf.d/oracle.conf  

  添加
  

/usr/lib/oracle/10.2.0.3/client64/lib/  

  This tells ldconfig to also look for libraries in the lib folder of the Instant Client installation. To update the library cache just call ldconfig without any parameter. This will take a while since ldconfig will re-read every configured library folder and add its content to the library cache. The new oracle.conf file has to be owned by the root user as well as ldconfig has to be called as the root user. Afterwards so should be able to use the cx_Oracle module in your Python shell:
  这样做之后,有时仍然出现问题。参照这里的方法,需要设置环境变量:
  方法1:
  
export PATH
  
在/etc/profile文件中添加变量,该变量将会对Linux下所有用户有效,并且是“永久的”。
  

vi /etc/profile  

  在文件末尾添加
  

LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/lib/oracle/10.2.0.3/client64/lib  
export LD_LIBRARY_PATH
  

  要是刚才的修改马上生效,需要执行以下代码
  

source /etc/profile  

  这时再查看系统环境变量,就能看见刚才加的东西已经生效了
  

echo $LD_LIBRARY_PATH  

  方法2: 直接运行export命令定义变量,只对当前shell(BASH)有效(临时的)
  

export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/lib/oracle/10.2.0.3/client64/lib  

问题 2
  

import cx_Oracle  
ImportError: No module named cx_Oracle
  

  如果安装的 python 64 位,需要把cx_Oracle文件复制到 /usr/lib64/python2.7/site-packages/ 目录下
  

cd /usr/lib/python2.7/site-packages/  
cp cx_Oracle.so /usr/lib64/python2.7/site-packages/cx_Oracle.so
  
cp cx_Oracle-5.1.2-py2.7.egg-info /usr/lib64/python2.7/site-packages/cx_Oracle-5.1.2-py2.7.egg-info
  

  如果是 Ubuntu 系统则需要注意
  For Debian and derivatives, this sys.path is augmented with directories for packages distributed within the distribution. Local addons go into /usr/local/lib/python/dist-packages, Debian addons install into /usr/{lib,share}/python/dist-packages. /usr/lib/python/site-packages is not used.
  

cd /usr/lib/python2.7  
sudo mv site-packages/cx_Oracle* dist-packages/
  
sudo rmdir site-packages/
  
sudo ln -s dist-packages site-packages
  
sudo ldconfig
  
  
Thanks to http://bayo.opadeyi.net/2011/07/setting-up-cxoracle-on-ubuntu-1104.html
  

SQLAlchemy Oracle 的中文问题
  你需要设置 NLS_LANG 环境变量,否则你读取出来的中文可能是乱码,或者当 insert 的数据有中文时会导致 Unicode 编码错误。
  你可以在 Python 代码中这么设置环境变量
  

# 设置编码,否则:  
# 1. Oracle 查询出来的中文是乱码
  
# 2. 插入数据时有中文,会导致
  
# UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-7: ordinal not in range(128)
  
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-145008-1-1.html 上篇帖子: Python::re 模块 下篇帖子: 2015/9/5 Python基础(9):条件和循环
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表