对于Oracle客户端,不只需要安装相应的python模块,还需要安装Oracle Client;还需要配置tnsnames.ora(当然也可以简单的通过host:port/instance访问)。
1、下载安装cx_Oracle python模块
[root@master pystudy]#wget https://pypi.python.org/packages/3b/67/d98ecdeb96cd766c69b9aca90d874a26bb4d75d79b71cc15d47dc8f35200/cx_Oracle-5.2.1-12c-py26-1.x86_64.rpm#md5=0327490cd98232721ca3dca1de49a064
[root@master pystudy]# rpm -ivh cx_Oracle-5.2.1-12c-py26-1.x86_64.rpm
2、安装Oracle客户端并配置环境变量
安装Oracle客户端,并在使用Python调用cx_Oracle模块的Linux用户下,配置Oracle环境变量。
[root@master pystudy]# cat /etc/profile
......
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=$ORACLE_BASE/product/12.1.0/db_1
ORACLE_SID=orcl
PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin
LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib
export ORACLE_BASE ORACLE_HOME ORACLE_SID PATH LD_LIBRARY_PATH
[root@master pystudy]# source /etc/profile
3、验证
[root@master pystudy]# python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_oracle
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named cx_oracle
>>> import cx_Oracle
>>>
ok!