Mac OS 下安装 Python3.6 cx_Oracle

操作系统版本:
macOS High Sierra 10.13 Beta
Python版本:
Python 3.6.0
macOS 默认Python 版本为2.7,为了不更改系统默认版本,可采用官网 pkg 安装包安装 Python3。https://www.python.org/downloads/


Python3 cx_Oracle 测试代码:

import cx_Oracle as orcl

print("cx_Oracle.version:", orcl.version)

host = "192.168.1.250"
port = "1521"
sid = "ORCL"
dsn = orcl.makedsn(host, port, sid)
connection = orcl.connect("odb1", "odb1", dsn)
cursor = orcl.Cursor(connection) 

print("======")
sql = "select count(1) as total from ACCOUNT"
res = cursor.execute(sql)
print(res.fetchone())
print("======")


安装 cx_Oracle

https://pypi.python.org/pypi/cx_Oracle/
目前 cx_Oracle 版本为 6.0rc1 ,可以采用 pip 进行安装
https://oracle.github.io/python-cx_Oracle/

pip3 install cx_Oracle --pre

(因在 mac 下安装的 python3 所以需使用 pip3 进行安装)

安装完成后可以运行代码进行测试,报错如下:

Traceback (most recent call last):
  File "/Users/wanghuan/iCode/Python/HelloPython/runorcale.py", line 1, in 
    import cx_Oracle
cx_Oracle.DatabaseError: DPI-1047: Oracle Client library cannot be loaded: dlopen(libclntsh.dylib, 1): image not found. See https://oracle.github.io/odpi/doc/installation.html for help

根据提示查看:https://oracle.github.io/odpi/doc/installation.html
找到 macOS 安装说明 :
Download the 11.2 or 12.1 “Basic” or “Basic Light” zip file from here. Choose either a 64-bit or 32-bit package, matching your application architecture. Most applications use 64-bit.

需下载相应的 Instant Client Package 包文件,点击 here 进入 http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html

找到相应的版本,我下载的是 instantclient-basic-macos.x64-11.2.0.4.0.zip (此处下载需要有 Oracle 网站用户账号,根据提示免费注册即可,登录后回到下载页,同意协议,进行下载即可)

下载完成后根据 https://oracle.github.io/odpi/doc/installation.html#macos 说明进行安装

在终端中通过命令在 home 下新建 lib 文件夹 mkdir ~/lib, 然后将下载的zip包解压,全部文件复制到 ~/lib 文件夹下即可


测试代码运行结果如下:

cx_Oracle.version: 6.0rc1
======
(166079,)
======

如果出现cx_Oracle.DatabaseError: ORA-21561: OID generation failed错误,需要配置 hosts 文件
通过终端查看 hostname

hostname
xxxMacBook.local 

将 hostname 结果填加至 /etc/hosts 最后一行即可

sudo vim /etc/hosts
127.0.0.1  xxxMacBook.local

你可能感兴趣的:(Mac OS 下安装 Python3.6 cx_Oracle)