问题
使用Python3 for mac上面的cx_Oracle数据库驱动
步骤
下载Oracle的Instant Client 程序包
这里版本的选择,应该根据你访问的oracle库的版本来选择。
配置环境变量
ORACLE_HOME=/usr/local/oracle/instantclient_11_2LD_LIBRARY_PATH=$ORACLE_HOMEVERSION=11.2.0.3.0ARCH=x86_64DYLD_LIBRARY_PATH=/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/clidriver/lib:$ORACLE_HOMEexport ORACLE_HOMEexport LD_LIBRARY_PATHexport VERSIONexport ARCHexport DYLD_LIBRARY_PATH
**Note:**这里的DYLD_LIBRARY_PATH需要留意,如果这里配置失误,会导致Python找不到相关库,如下错误:
>>> import cx_OracleTraceback (most recent call last): File "", line 1, in ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cx_Oracle.cpython-36m-darwin.so, 2): Library not loaded: /ade/b/2649109290/oracle/rdbms/lib/libclntsh.dylib.11.1 Referenced from: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cx_Oracle.cpython-36m-darwin.so Reason: image not found
安装Oracle的Instant Client 程序包
mkdir -p /usr/local/oracle/cd /usr/local/oracle/tar -xzf instantclient-basic-macos.x64-11.2.0.3.0.ziptar -xzf instantclient-sdk-macos.x64-11.2.0.3.0.zipcd instantclient_11_2ln -s libclntsh.dylib.11.1 libclntsh.dylibln -s libocci.dylib.11.1 libocci.dylib
安装cx_Oracle驱动
env ARCHFLAGS="-arch $ARCH" pip3 install cx_Oracle
检查cx_Oracle驱动是否安装成功
zylMBP:instantclient_11_2 zhangyalin$ python3Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import cx_Oracle>>> exit()
若出现,如下情况,则需要回头检查DYLD_LIBRARY_PATH环境变量:
zylMBP:instantclient_11_2 zhangyalin$ python3Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import cx_OracleTraceback (most recent call last): File "", line 1, in ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cx_Oracle.cpython-36m-darwin.so, 2): Library not loaded: /ade/b/2649109290/oracle/rdbms/lib/libclntsh.dylib.11.1 Referenced from: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cx_Oracle.cpython-36m-darwin.so Reason: image not found
总结
希望pip越来越好用,其实主要是我对Oracle和Python不熟悉,这里的安装需要事想配置好oracle的SDK才能够比较顺利的使用pip安装oracle的驱动。
参考: