環(huán)境
macOS 10.14.5
Python3.7
一:安裝cx_Oracle
注意Mac自帶Python2涮雷,安裝時不要裝錯了,可以在PyCharm中直接安裝避免按錯到Python2上级乍。
方法一:
- 到PyCharm>>Preferences>>Project:newPro>>Project Interpreter中Project Interpreter選擇 Python3.7:然后點擊“+”晓避,并搜索cx-Oracle;
- 然后點擊Install Package
如果報以下錯誤
“xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun”
需升級xcode或執(zhí)行以下命令:
xcode-select --install
方法二:
直接在終端中執(zhí)行:
python3 -m pip install cx_Oracle --upgrade #這里要注意是給Python3安裝
如果也報以下錯誤
“xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun”
還是需要升級xcode或執(zhí)行以下命令:
xcode-select --install
如果提示“Consider using the --user
option or check the permissions.”可嘗試執(zhí)行以下命令,然后輸入密碼:
sudo python3 -m pip install cx_Oracle --upgrade
二:下載oracle instant client zip
到oracle官網(wǎng)下載instantclient-basic-macos.x64-18.1.0.0.0.zip:
https://www.oracle.com/database/technologies/instant-client/macos-intel-x86-downloads.html
不過需要先注冊才可以下載禽炬。
三:安裝oracle instant client zip
下載后打開終端:
- 新建一個文件夾
??sudo mkdir -p /opt/oracle
- 將下載的oracle instant client zip移動到剛建好的文件夾中
sudo mv /Users/{UserName}/Downloads/instantclient-basic-macos.x64-18.1.0.0.0.zip /opt/oracle/
- 切換到該目錄下
cd /opt/oracle/
- 解壓文件
sudo unzip instantclient-basic-macos.x64-18.1.0.0.0.zip
- 再新建一個文件夾
mkdir ~/lib
- 建立連接
ln -s /opt/oracle/instantclient_18_1/libclntsh.dylib ~/lib/
- 復制OCI庫
cp /opt/oracle/instantclient_18_1/{libclntsh.dylib.18.1,libclntshcore.dylib.18.1,libons.dylib,libnnz18.dylib,libociei.dylib} ~/lib/
- Instant Client鏈接的應用程序的默認Oracle配置目錄
mkdir -p /opt/oracle/instantclient_18_1/network/admin
四植阴、測試是否成功導入
在Python交互模式下import cx_Oracle:
>>>import cx_Oracle
如果沒報錯就表示安裝成功蟹瘾。
嘗試連接數(shù)據(jù)庫:
>>>cx_Oracle.connect('user/password@dsn') #如:cx_Oracle.connect('user/password@127.0.0.1:1521/orcl') 記得加/orcl
翻了其他人的總結,有的人更改了注冊表配置了環(huán)境變量掠手,但是我沒改