pip install cx_oracle,使用pip安装 cx_Oracle 模块

1. 系统环境,linux, python3.5, pip

以下命令的执行均使用root用户。

2. 执行 pip install cx_Oracle

出错,提示不能定位Oracle的安装。出现该问题的原因是因为没有安装Oracle客户端相关的库文件。

下载文件:instantclient-basic-linux.x64-12.1.0.2.0.zip

下载链接:http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html

3. 解压下载到的文件,假设解压到 /opt/instantclient_12_1/ 这个目录

设置环境变量:export ORACLE_HOME=/opt/instantclient_12_1/

再次执行 pip install cx_Oracle

出错,提示编译过程未找到 oci.h 这个头文件。

4. 下载文件:instantclient-sdk-linux.x64-12.1.0.2.0.zip

下载链接:一致

解压下载到的文件,假设仍然解压到 /opt/instantclient_12_1/

5. 添加头文件的搜索路径:

export C_INCLUDE_PATH=/opt/instantclient_12_1/sdk/include/

再次执行:pip install cx_Oracle

编译成功,链接出错,提示 cannot find -lclntsh

6. 添加库搜索路径

export LD_LIBRARY_PATH=/opt/instantclient_12_1/

再次执行 pip install cx_Oracle

依然提示未找到

猜测是名称问题,于是建立符号链接:

ln -s /opt/instantclient_12_1/libclntsh.so.12.1 /opt/instantclient_12_1/libclntsh.so

再次执行 pip install cx_Oracle

安装成功。

7. 另开一个 terminal,在 python 中执行 import cx_Oracle 提示未找到共享库。

应该是之前设置的环境变量失效了,故修改用户的配置文件:~/.bashrc

在里面加一行:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/instantclient_12_1/

8. 配置完成

0. 总结:

使用 pip 安装 cx_Oracle 的过程中要 检测 instantclient,编译源码,进行安装。

其中环境变量 ORACLE_HOME 用于表示 instantclient 的安装位置,

C_INCLUDE_PATH 供 gcc 查找 头文件的路径,

LD_LIBRARY_PATH 用于 cx_Oracle的链接阶段和执行阶段。

原文:http://www.cnblogs.com/vanwoos/p/5928116.html

你可能感兴趣的:(pip,install,cx_oracle)