macOS搭建Python的cx_Oracle開發(fā)環(huán)境

macOS搭建Python的cx_Oracle開發(fā)環(huán)境的過程總結(jié)。

環(huán)境與版本

OS: macOS High Sierra 10.13.6

Oracle Instant Client: Version 12.2.0.1.0 (64-bit) Updated 15-Jan-2018

安裝Oracle Instant Client,

Oracle網(wǎng)站的下載需要注冊一個(gè)賬戶,根據(jù)macOS版本選擇Oracle Instant Client,自行申請下載。

https://www.oracle.com/technetwork/topics/intel-macsoft-096467.html

macOS版本 Oracle Instant Client版本
macOS High Sierra, Sierra and El Capitan Instant Client 12.2
macOS El Capitan, Yosemite and Mavericks Instant Client 12.1
macOS X Lion, Mountaint Lion and Mavericks Instant Client 11.2

下載所需要的文件,

basic: instantclient-basic-macos.x64-12.2.0.1.0-2.zip

sqlplus: instantclient-sqlplus-macos.x64-12.2.0.1.0-2.zip

sdk: instantclient-sdk-macos.x64-12.2.0.1.0-2.zip

解壓包到一個(gè)文件夾里,如/usr/local/lib/Oracle/instantclient_12_2,

unzip instantclient-basic-macos.x64-12.2.0.1.0-2.zip # 提供基本功能
unzip instantclient-sqlplus-macos.x64-12.2.0.1.0-2.zip # 提供sqlplus命令行
unzip instantclient-sdk-macos.x64-12.2.0.1.0-2.zip # 提供頭文件,cx_Oracle會(huì)依賴其中的oci.h

鏈接庫文件

添加到$HOME/lib/usr/local/lib的鏈接,以使應(yīng)用程序能夠找到庫。

ln -s /usr/local/lib/Oracle/instantclient_12_2/libclntsh.dylib.12.1 /usr/local/lib

如果您打算將可選的Oracle配置文件(如tnsnames.ora,sqlnet.ora或oraaccess.xml)與Instant Client放在一起,則創(chuàng)建一個(gè)network/admin子目錄(如果該子目錄不存在)。

mkdir -p /usr/local/lib/Oracle/instantclient_12_2/network/admin

增加環(huán)境變量,

export PATH=/usr/local/lib/Oracle/instantclient_12_2:$PATH # sqlplus工具也可以使用了

安裝cx_Oracle,

pip install cx_Oracle

驗(yàn)證,

>>> import cx_Oracle
>>> db = cx_Oracle.connect('hr', 'hrpwd', 'localhost:1521/XE')
>>> db1 = cx_Oracle.connect('hr/hrpwd@localhost:1521/XE')
>>> dsn_tns = cx_Oracle.makedsn('localhost', 1521, 'XE')
>>> print(dsn_tns)
(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SID=XE)))
>>> db2 = cx_Oracle.connect('hr', 'hrpwd', dsn_tns)

參考文檔:

https://www.oracle.com/technetwork/topics/intel-macsoft-096467.html

https://oracle.github.io/odpi/doc/installation.html#macos

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容