Mac OS X Capitan安装cx_Oracle

 前言

      借用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//oracle
mv /Users//Downloads/instantclient-* /Users//oracle
cd /Users//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//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//oracle/instantclient_11_2

 

   这是为了防止直接安装而造成的错误:

#执行包含以下python代码的文件:
import cx_Oracle
#错误信息如下:
Traceback (most recent call last):
  File "ex1.py", line 1, in 
    import cx_Oracle
  File "build/bdist.macosx-10.11-intel/egg/cx_Oracle.py", line 7, in 
  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//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 
    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_目录。

 

  参考:

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

你可能感兴趣的:(Python,cx_Oracle,Mac,OS,X,Capitan,zsh,pip,image,not,found)