Python cx_Oracle 安装小记
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新。请访问我的个人网站获取这篇文章的最新内容,Python cx_Oracle 安装小记
SQLAlchemy 是 Python 中用来操作数据库的一个利器,支持 MySQL、Oracle、PostgreSQL、SQLite、Oracle。使用 SQLAlchemy 来管理 Oracle 的数据需要安装依赖 cx_Oracle。在这过程中遇到不少问题,记录如下。
cx_Oracle 可以到这里下载:http://cx-oracle.sourceforge.net/
旧版本的下载地址:http://sourceforge.net/projects/cx-oracle/files/
现在也可以通过 pip install cx_Oracle 安装
Windows
安装 cx_Oracle 正确版本,需要区分 10g, 11g,安装 instantclient_11_2(请到 Oracle 官网下载)
问题
ImportError: DLL load failed: 找不到指定的模块。
将 instantclient_11_2 所在的目录添加到环境变量,但是环境变量有时没有立即生效
,可以复制 oci.dll(版本也要正确)到 \Python27\Lib\site-packages 目录下
这时又出现问题
Unable to acquire Oracle environment handle,
复制 oci.dll 依赖的 dll,oraociei11.dll,ocijdbc11.dll 到 \Python27\Lib\site-packages 目录下。如果不清楚就将 instantclient_11_2 目录下的 dll 都复制到 \Python27\Lib\site-packages 目录下
可以参考:http://docs.oracle.com/cd/B12037_01/appdev.101/b10779/oci01int.htm#423364
# UNIX
libclnstsh.so.10.1
libnnz10.so
libociei.so
# Windows
oci.dll
oraociei10.dll
orannzsbb10.dll
Linux
# 先安装 rpm
yum install rpm
# oracle-instantclient-basic-10.2.0.3-1.x86_64.rpm 请到 Oracle 官网下载
rpm -ivh oracle-instantclient-basic-10.2.0.3-1.x86_64.rpm
rpm -ivh cx_Oracle-5.1.2-10g-py27-1.x86_64.rpm
# 有这个文件表示安装成功,根据 python 的位置,也可能在其他地方
ls /usr/lib/python2.7/site-packages/cx_Oracle.so
问题 1
import cx_Oracle
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libclntsh.so.10.1: cannot open shared object file: No such file or directory
The problem is that the just installed libraries of the Oracle Instant Client aren’t part of the default library path of your CentOS installation. Either you have to extend the LD_LIBRARY_PATH Bash variable of your current user or you have to add the lib directory of the Instant Client installation to the system-wide library path if all users should be allowed to use the Oracle Instant Client. To do so you have to create a new file, e.g. oracle.conf, in the /etc/ld.so.conf.d directory with the following content:
vim /etc/ld.so.conf.d/oracle.conf
添加
/usr/lib/oracle/10.2.0.3/client64/lib/
This tells ldconfig to also look for libraries in the lib folder of the Instant Client installation. To update the library cache just call ldconfig without any parameter. This will take a while since ldconfig will re-read every configured library folder and add its content to the library cache. The new oracle.conf file has to be owned by the root user as well as ldconfig has to be called as the root user. Afterwards so should be able to use the cx_Oracle module in your Python shell:
这样做之后,有时仍然出现问题。参照这里的方法,需要设置环境变量:
方法1:
export PATH
在/etc/profile文件中添加变量,该变量将会对Linux下所有用户有效,并且是“永久的”。
vi /etc/profile
在文件末尾添加
LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/lib/oracle/10.2.0.3/client64/lib
export LD_LIBRARY_PATH
要是刚才的修改马上生效,需要执行以下代码
source /etc/profile
这时再查看系统环境变量,就能看见刚才加的东西已经生效了
echo $LD_LIBRARY_PATH
方法2: 直接运行export命令定义变量,只对当前shell(BASH)有效(临时的)
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/lib/oracle/10.2.0.3/client64/lib
问题 2
import cx_Oracle
ImportError: No module named cx_Oracle
如果安装的 python 64 位,需要把cx_Oracle文件复制到 /usr/lib64/python2.7/site-packages/ 目录下
cd /usr/lib/python2.7/site-packages/
cp cx_Oracle.so /usr/lib64/python2.7/site-packages/cx_Oracle.so
cp cx_Oracle-5.1.2-py2.7.egg-info /usr/lib64/python2.7/site-packages/cx_Oracle-5.1.2-py2.7.egg-info
如果是 Ubuntu 系统则需要注意
For Debian and derivatives, this sys.path is augmented with directories for packages distributed within the distribution. Local addons go into /usr/local/lib/python/dist-packages, Debian addons install into /usr/{lib,share}/python/dist-packages. /usr/lib/python/site-packages is not used.
cd /usr/lib/python2.7
sudo mv site-packages/cx_Oracle* dist-packages/
sudo rmdir site-packages/
sudo ln -s dist-packages site-packages
sudo ldconfig
Thanks to http://bayo.opadeyi.net/2011/07/setting-up-cxoracle-on-ubuntu-1104.html
SQLAlchemy Oracle 的中文问题
你需要设置 NLS_LANG 环境变量,否则你读取出来的中文可能是乱码,或者当 insert 的数据有中文时会导致 Unicode 编码错误。
你可以在 Python 代码中这么设置环境变量
# 设置编码,否则:
# 1. Oracle 查询出来的中文是乱码
# 2. 插入数据时有中文,会导致
# UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-7: ordinal not in range(128)
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
Python cx_Oracle 安装小记的更多相关文章
- YCM 安装小记
layout: post title: YCM 安装小记 半夜,女朋友在那边抱怨购物车的物品秒无货,我这边刚好成功安装了vim上最难装的插件--YouCompleteMe,内心非常激动,于是本着取之于 ...
- Python的安装和详细配置
Python是一种面向对象.解释型计算机程序设计语言.被认为是比较好的胶水语言.至于其他的,你可以去百度一下.本文仅介绍python的安装和配置,供刚入门的朋友快速搭建自己的学习和开发环境.本人欢迎大 ...
- python requests 安装
在 windows 系统下,只需要输入命令 pip install requests ,即可安装. 在 linux 系统下,只需要输入命令 sudo pip install requests ,即可 ...
- Python 的安装与配置(Windows)
Python2.7安装配置 python的官网地址:https://www.python.org/ 我这里下载的是python2.7.12版本的 下载后点击安装文件,直接点击下一步知道finally完 ...
- 初学python之安装Jupyter notebook
一开始安装python的时候,安装的是最新版的python3.6的最新版.而且怕出问题,选择的都是默认安装路径.以为这样总不会出什么问题.一开始确实这样,安装modgodb等一切顺利.然而在安装jup ...
- 转: python如何安装pip和easy_installer工具
原文地址: http://blog.chinaunix.net/uid-12014716-id-3859827.html 1.在以下地址下载最新的PIP安装文件:http://pypi.python. ...
- CentOS 6.5升级Python和安装IPython
<转自:http://www.noanylove.com/2014/10/centos-6-5-sheng-ji-python-he-an-zhuang-ipython/>自己常用.以做备 ...
- python Scrapy安装和介绍
python Scrapy安装和介绍 Windows7下安装1.执行easy_install Scrapy Centos6.5下安装 1.库文件安装yum install libxslt-devel ...
- window下从python开始安装科学计算环境
Numpy等Python科学计算包的安装与配置 参考: 1.下载并安装 http://www.jb51.net/article/61810.htm 1.安装easy_install,就是为了我们安装第 ...
随机推荐
- jmeter性能测试的小小实践
项目描述: 被测试网站: www.cnblogs.com 指标:响应时间及错误率 场景:线程数--20: Ramp-Up period(in second 10)--10: 循环次数--10 测试步骤 ...
- 学习python的字符串的方法
今天看了下学习视频,发现str也有许多方法,才发现昨天了解的关于字符串知识甚少,所以今天查看了python的源码,学习一些新的方法 以下是我把边对照着源码边翻译,边尝试实现方法 1.第一个字符有大写字 ...
- yarn不是内部指令 react-native不是内部指令
1.先查看是否全局安装 2.我遇到的是,全局安装了,依然有问题. 昨天装了一个高版本的node,成功后有个黑窗口我点了几个回车,在我的环境变量里加了一大推重复的变量,删除就正常了,如下图是我删完之后的
- SpringBoot下如何配置实现跨域请求?
一.什么是跨域请求? 跨域请求,就是说浏览器在执行脚本文件的ajax请求时,脚本文件所在的服务地址和请求的服务地址不一样.说白了就是ip.网络协议.端口都一样的时候,就是同一个域,否则就是跨域.这是由 ...
- vue day3 bootstrap 联动下拉
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta ht ...
- PythonStudy——python中如何使输出不换行
1.在python 3.x版本中,使用print(,end="") 可使输出不换行, 例如:
- JDK 8 安装及配置
1.配置java环境变量 注意:jdk文件夹名字取名不要用汉语取名. 1)鼠标右键点击我的电脑(计算机)选择属性栏 2)再点击左边高级系统设置 3)点击环境变量 4)在用户变量窗口新建变量名为JAVA ...
- debian系统下apache2开启ssi功能
SSI (Server Side Include)的 html 文件扩展名 (.shtml), 通常称为"服务器端嵌入"或者叫"服务器端包含"说白了就是类似其他 ...
- OpenStack上搭建Q版的公共环境准备(step1)
vmware14 centos7.5minimal版 controller1节点虚拟硬件配置: CPU:1颗2核 Memory:2G 硬盘:20G 网卡: VMnet1(仅主机模式):关闭DHCP,手 ...
- Python模块 os和sys
os模块是与操作系统交互的一个接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相 ...