---
title: "MacOS Python 操作 Oracle——安装cx_Oracle模块"
author: "jashawn"
date: "2012-08-22T15:08:01Z"
tags: ["MacOS", "Oracle", "Python"]
categories: ["数据库"]
slug: "macos-python-oracle-cx-oracle"
draft: false
lastmod: "2012-08-22T15:08:56Z"
id: 1654441
url: "https://jashawn.iteye.com/blog/1654441"
---

**1. 准备**

a) 确保已经从Xcode安装了命令行工具

b) 到 [Oracle](http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html)  以下载两个文件

instantclient-basic-1.0.xx-macos-x64.zip

instantclient-sdk-1.0.xx-macos-x64.zip

c) 创建一个目录，我这里是/opt/oracle

d) 将instantclient-basic-1.0.xx-macos-x64.zip解压到以上文件夹，得到/opt/oracle/instantclient\_10\_2

e) 将instantclient-sdk-1.0.xx-macos-x64.zip里面的sdk目录解压到上面的文件夹，为/opt/oracle/instantclient\_10\_2/sdk

f) 到sdk目录下把ottclasses.zip解压出来，它会产生一个oracle目录

g) 回到/opt/oracle/instantclient\_10\_2复制类库文件到当前目录

cp ./sdk/* .

cp ./sdk/includ .

h) 类库OK，设置几个环境变量，编辑/etc/profile

ORACLE\_HOME = /opt/oracle/instantclient\_10\_2

DYLD\_LIBRARY\_PATH = $ORACLE\_HOME

LD\_LIBRARY\_PATH = $ORACLE\_HOME

i) 链接两个文件：

ln -s libclntsh.dylib.10.1 libclntsh.dylib

ln -s libocci.dylib.10.1 libocci.dylib

现在重启机器或者source /etc/profile

**2. 安装cx\_oracle**

下载 [cx\_Oracle](http://cx-oracle.sourceforge.net/)

安装cx\_Oracle时我是切换到root完成的安装，否则出现没有权限，有些资料说不需要，反正我没有成功。执行下面命令安装，

 

python setup.py build

python setup.py install

 

安装完了之后测试一下cx\_Oracle

$ python

&gt;&gt;&gt; import cx\_Oracle

可能出错误，找不到符号 \_OCIAttrGet，貌似说是MacOS上的Python是64位的而cx\_Oracle是32位的，

在运行python命令之前先设置一个值，

$ export VERSIONER\_PYTHON\_PREFER\_32\_BIT=Yes

 

$ python

&gt;&gt;&gt; import cx\_Oracle

应该不再报错了。

 

**3. IDE设置**

主要是想办法填加以上几个环境变量

 

ORACLE\_HOME = /opt/oracle/instantclient\_10\_2

DYLD\_LIBRARY\_PATH = $ORACLE\_HOME

LD\_LIBRARY\_PATH = $ORACLE\_HOME

VERSIONER\_PYTHON\_PREFER\_32\_BIT=Yes

 

TextMate可以在设置的shell variables里设置，

aptana 里PyDev-&gt;Interpreter Python的Environment里填加