mac上安装python3的cx_Oracle数据库驱动

不羁岁月 提交于 2019-12-01 15:21:52

问题

使用Python3 for mac上面的cx_Oracle数据库驱动

步骤

下载Oracle的Instant Client 程序包

Instant Client 下载适用于 Mac OS X (Intel x86)

下载basic和sdk两个安装包

这里版本的选择,应该根据你访问的oracle库的版本来选择。

配置环境变量

ORACLE_HOME=/usr/local/oracle/instantclient_11_2
LD_LIBRARY_PATH=$ORACLE_HOME
VERSION=11.2.0.3.0
ARCH=x86_64
DYLD_LIBRARY_PATH=/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/clidriver/lib:$ORACLE_HOME
export ORACLE_HOME
export LD_LIBRARY_PATH
export VERSION
export ARCH
export DYLD_LIBRARY_PATH

**Note:**这里的DYLD_LIBRARY_PATH需要留意,如果这里配置失误,会导致Python找不到相关库,如下错误:

>>> import cx_Oracle
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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.zip
tar -xzf instantclient-sdk-macos.x64-11.2.0.3.0.zip
cd instantclient_11_2
ln -s libclntsh.dylib.11.1 libclntsh.dylib
ln -s libocci.dylib.11.1 libocci.dylib

安装cx_Oracle驱动

env ARCHFLAGS="-arch $ARCH" pip3 install cx_Oracle

检查cx_Oracle驱动是否安装成功

zylMBP:instantclient_11_2 zhangyalin$ python3
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> exit()

若出现,如下情况,则需要回头检查DYLD_LIBRARY_PATH环境变量:

zylMBP:instantclient_11_2 zhangyalin$ python3
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
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的驱动。

参考: Installing cx_Oracle for Python/ Mac OSX Install Oracle for Python on OSX.md

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!