您的位置:首页 > 数据库 > Oracle

linux安装cx_oracle

2015-09-01 15:35 501 查看
1、下载oracle
instant client并安装

2、配置oracle环境

$unzip basic-11.1.0.70-linux-x86_64.zip
$cd instantclient_11_1
$cp * /usr/lib   #直接放到动态库搜索路径中,不需要额外的环境配置

或
$unzip basic-11.1.0.70-linux-x86_64.zip
$cp -rf instantclient_11_1 /opt/
$vi /etc/profile
export ORACLE_HOME=/opt/instantclient_11_1
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME

$source /etc/profile
3、pip --trusted-host pypi.python.org install cx_oracle安装即可
4、连接验证

1

2

$python

>>import
cx_Oracle

若报错:没有cx_Oracle这个模块,但是cx_Oracle已经安装上了,说明python没有找到cx_Oracle.so这个文件.

cx_Oracle.so安装在python的site-packages这个目录下了.安装完python后Linux上存在两个这样的目录:

一个是:/usr/lib/python27/site-packages

另一个是:/usr/local/lib/python27/site-packages

12345>>> import sys>>> print sys.path['', 'C:\\windows\\SYSTEM32\\python27.zip', 'D:\\Python27\\DLLs', 'D:\\Python27\\lib', 'D:\\Python27\\lib\\plat-win', 'D:\\Python27\\lib\\lib-tk', 'D:\\Python27', 'D:\\Python27\\lib\\site-packages']
看下当前的python所指向的site-packages是哪一个,然后将cx_oracle.so复制过去就可以了. 若报错: import cx_Oracle gave ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory表示没有找到instant client的动态库,check一下环境变量是否配置,是否生效,版本是否正确。看看是不是改完/etc/profile 后忘记 source 一下了.若报错:ImportError: ./cx_Oracle.so: undefined symbol: PyUnicodeUCS4_Decode

1

Google的信息:There
<span
class="keyword"
style="font-weight:
bold;">is</span>
nothing
wrong
with
Debian.
Python
supports
two
incompatible modes
of
operation
<span
style="color:
#0000ff;"><span
class="keyword"
style="font-weight:
bold; color: #333333;">for</span></span>
Unicode,
UCS2
(the
<span
style="color:
#0000ff;"><span
class="keyword"
style="font-weight:
bold; color: #333333;">default</span></span>),
and
UCS4.
Debian uses
the
<span
style="color:
#0000ff;"><span
class="keyword"
style="font-weight:
bold; color: #333333;">default</span></span>,
Redhat
uses
UCS4.
You
need
to
recompile
the
extension <span
style="color:
#0000ff;"><span
class="keyword"
style="font-weight:
bold; color: #333333;">for</span></span>
UCS-<span
class="number"
style="color:
#009999;">2</span>
mode
(i.e.
<span
class="keyword"
style="font-weight:
bold;">using</span>
a
Debian
installation);
<span
class="keyword"
style="font-weight:
bold;">this</span>
would
fix
the
undefined
symbol:
PyUnicodeUCS4_Decode

所以重新编译python

1

./configure
--enable-shared
--prefix=/usr/local/python27
-enable-unicode=ucs4

若是报:error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory



ImportError: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

这是因为:

由于在系统的lib路径中找不到这个共享库。

注: 如果编译时加上了--enable-shared,才会编译这个共享库,默认的位置是python可执行程序所在目录的lib目录下,如/usr/local/python27

解决办法:

1. 可以使用如下方式编译Python以解决这个问题:

./configure --enable-shared --prefix=/usr/local/python27

make && make install

2. cp /usr/local/python27/lib/libpython2.7.so.1.0 /usr/local/lib

cd /usr/local/lib

ln -s libpython2.7.so.1.0 libpython2.7.so

3. 使用命令whereis libpython2.7.so.1.0得到如下结果就说明

libpython2.7.so.1: /usr/local/lib/libpython2.7.so.1.0

4. 如果whereis没有结果,或者还有import错误,可以尝试如下操作:

在/etc/ld.so.conf中加入新行/usr/local/lib

保存后,运行

/sbin/ldconfig

/sbin/ldconfig –v

再次验证,终于正常import了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: