前言
借用Joel Vasallo在其部落格《Installing cx_Oracle on a Mac》中所說:
So as previously mentioned, I got a Macbook Pro. More than four months later, I am loving it more and more. The only gripe I had was installing a specific package called cx_Oracle; a Python Oracle DB connection package. After a lot of attempts, I finally got a working copy installed on my local machine. I noticed there are many tutorials around the web, but they are a bit outdated, I am on Mavericks, so I will create a nice article not only for you guys, but also for myself.
環境:
系統:OS X EI Capitan v10.11.3
CPU:2.7 GHz Intel Core i5
需要下載下傳的内容(請去oracle官網下,沒有賬号就先注冊一個):
instantclient-basic-macos.x64-11.2.0.4.0.zip
instantclient-sdk-macos.x64-11.2.0.4.0.zip
解壓和移動檔案:
mkdir /Users/<username_here>/oracle
mv /Users/<username_here>/Downloads/instantclient-* /Users/<username_here>/oracle
cd /Users/<username_here>/oracle
unzip instantclient-basic-macos.x64-11.2.0.4.0.zip
unzip instantclient-sdk-macos.x64-11.2.0.4.0.zip
cd instantclient_11_2/sdk
unzip ottclasses.zip
cd ..
cp -R ./sdk/* .
cp -R ./sdk/include/* .
ln -s libclntsh.dylib.11.1 libclntsh.dylib
ln -s libocci.dylib.11.1 libocci.dylib
配置環境變量:
vim ~/.bash_profile
export ORACLE_HOME=/Users/<username_here>/oracle/instantclient_11_2
export DYLD_LIBRARY_PATH=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME
export PATH=$PATH:$ORACLE_HOME
source ~/.bash_profile
提示一下,如果使用的不是預設的BASH,而使用的的是ZSH,請确認:~/.zshrc是否加載了~/.bash_profile,也就是~/.zshrc是否寫了“source ~/.bash_profile”,否則會找不到環境變量,因為ZSH啟動預設不加載~/.bash_profile。同時上訴腳本最後一句改為:
source ~/.zshrc
在目錄下執行以下内容來更改安裝資訊:
curl -O https://raw.githubusercontent.com/kubo/fix_oralib_osx/master/fix_oralib.rb
sudo ruby fix_oralib.rb --ic_dir /Users/<username_here>/oracle/instantclient_11_2
這是為了防止直接安裝而造成的錯誤:
#執行包含以下python代碼的檔案:
import cx_Oracle
#錯誤資訊如下:
Traceback (most recent call last):
File "ex1.py", line 1, in <module>
import cx_Oracle
File "build/bdist.macosx-10.11-intel/egg/cx_Oracle.py", line 7, in <module>
File "build/bdist.macosx-10.11-intel/egg/cx_Oracle.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/watson/.python-eggs/cx_Oracle-5.2.1-py2.7-macosx-10.11-intel.egg-tmp/cx_Oracle.so, 2): Library not loaded: /ade/b/3071542110/oracle/rdbms/lib/libclntsh.dylib.11.1
Referenced from: /Users/watson/.python-eggs/cx_Oracle-5.2.1-py2.7-macosx-10.11-intel.egg-tmp/cx_Oracle.so
Reason: image not found
安裝cx_Oracle,目前版本為v5.2.1, 沒有安裝pip的請先安裝pip:
sudo pip install --no-cache-dir --allow-external --allow-unverified cx_oracle
修改安裝後的内容:
sudo ruby fix_oralib.rb --ic_dir /Users/<username_here>/oracle/instantclient_11_2 /Library/Python/2.7/site-packages/cx_Oracle.so
這是為了解決以下問題:
#執行包含以下python代碼的檔案:
import cx_Oracle
#錯誤資訊如下:
Traceback (most recent call last):
File "ex1.py", line 1, in <module>
import cx_Oracle
ImportError: dlopen(/Library/Python/2.7/site-packages/cx_Oracle.so, 2): Library not loaded: @rpath/libclntsh.dylib.11.1
Referenced from: /Library/Python/2.7/site-packages/cx_Oracle.so
Reason: image not found
最後測試一下吧:
import cx_Oracle, string, getpass
def main():
# Get password
pswd = getpass.getpass()
# Build connection string
user = "CS327_jdoe"
host = "oracle.microlab.cs.utexas.edu"
port = "1521"
sid = "orcl"
dsn = cx_Oracle.makedsn (host, port, sid)
# Connect to Oracle and test
con = cx_Oracle.connect (user, pswd, dsn)
if (con):
print "Connection successful"
print con.version
else:
print "Connection not successful"
con.close()
main()
最後提示一下,目前版本需要建立$ORACLE_HOME/log/diag/clients目錄,以免~/目錄下出現莫名的oradiag_<user_name>目錄。
參考:
http://www.cs.utexas.edu/~mitra/csSpring2011/cs327/cx_mac.html
http://stackoverflow.com/questions/33259671/how-to-install-cx-oracle-on-el-capitan
http://joelvasallo.com/?p=276
https://sourceforge.net/p/cx-oracle/mailman/message/34534872/
http://stackoverflow.com/questions/3520054/what-is-oradiag-user-folder