mac 安装 cx oracle,Mac OS X Capitan安装cx_Oracle

本文档详细介绍了如何在Mac OS X EICapitan v10.11.3上安装cx_Oracle,包括下载Oracle Instant Client,配置环境变量,解决导入错误,并进行最终的测试。过程中涉及解压文件、移动文件到指定目录、修改环境变量、运行ruby脚本来修复库路径,以及安装和配置cx_Oracle库。文章还提供了遇到的错误信息及其解决方案,确保cx_Oracle能正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

借用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值