1. 前言
本文为python入坑记的第1篇文章。从2017年4月份接手公司的公募基金策略开发平台,也算正式入了Python的坑。入坑过程中发现Oracle的Python驱动安装简直是大坑,从网上搜索各种教程,被坑数次后神奇般的就安装上了。这里记录一下,做个备忘录。
2. 背景
问题:在MySQL大放异彩的今天,为什么要用Oracle数据库?
答:策略平台计算用户策略数据时,需要从代销系统的Oracle数据库拉取用户持仓数据。你问能不能把代销系统的数据库换成MySQL?答案是不能,因为代销系统太复杂了,而且是买的。
3. 开发环境
- CentOS6.6_x64
- Oracle: 11.2.0.1.0_64位
- Python: 3.4.4
4. 安装过程
- 从oracle官网下载2个rpm文件,官方说法叫
Instant Client Package
,注意版本,需要和Oracle版本对应。我们会发现有好几个Package,忽略其他的,这里我们只需要oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.rpm和oracle-instantclient11.2-devel-11.2.0.1.0-1.x86_64.rpm 。 - 先安装并查看
basic package
:
[root@dev ~]# rpm -ivh oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.rpm
Preparing... ########################################### [100%]
1:oracle-instantclient11.########################################### [100%]
[root@dev ~]# rpm -ql oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64
/usr/lib/oracle/11.2/client64/bin/adrci
/usr/lib/oracle/11.2/client64/bin/genezi
/usr/lib/oracle/11.2/client64/lib/libclntsh.so.11.1
/usr/lib/oracle/11.2/client64/lib/libnnz11.so
/usr/lib/oracle/11.2/client64/lib/libocci.so.11.1
/usr/lib/oracle/11.2/client64/lib/libociei.so
/usr/lib/oracle/11.2/client64/lib/libocijdbc11.so
/usr/lib/oracle/11.2/client64/lib/ojdbc5.jar
/usr/lib/oracle/11.2/client64/lib/ojdbc6.jar
/usr/lib/oracle/11.2/client64/lib/xstreams.jar
- 再安装并查看
dev package
:
[root@dev ~]# rpm -ivh oracle-instantclient11.2-devel-11.2.0.1.0-1.x86_64.rpm
Preparing... ########################################### [100%]
1:oracle-instantclient11.########################################### [100%]
[root@dev ~]# rpm -ql oracle-instantclient11.2-devel-11.2.0.1.0-1.x86_64
/usr/include/oracle/11.2/client64/ldap.h
/usr/include/oracle/11.2/client64/nzerror.h
/usr/include/oracle/11.2/client64/nzt.h
/usr/include/oracle/11.2/client64/occi.h
/usr/include/oracle/11.2/client64/occiAQ.h
/usr/include/oracle/11.2/client64/occiCommon.h
/usr/include/oracle/11.2/client64/occiControl.h
/usr/include/oracle/11.2/client64/occiData.h
/usr/include/oracle/11.2/client64/occiObjects.h
/usr/include/oracle/11.2/client64/oci.h
/usr/include/oracle/11.2/client64/oci1.h
/usr/include/oracle/11.2/client64/oci8dp.h
/usr/include/oracle/11.2/client64/ociap.h
/usr/include/oracle/11.2/client64/ociapr.h
/usr/include/oracle/11.2/client64/ocidef.h
/usr/include/oracle/11.2/client64/ocidem.h
/usr/include/oracle/11.2/client64/ocidfn.h
/usr/include/oracle/11.2/client64/ociextp.h
/usr/include/oracle/11.2/client64/ocikpr.h
/usr/include/oracle/11.2/client64/ocixmldb.h
/usr/include/oracle/11.2/client64/ocixstream.h
/usr/include/oracle/11.2/client64/odci.h
/usr/include/oracle/11.2/client64/oratypes.h
/usr/include/oracle/11.2/client64/ori.h
/usr/include/oracle/11.2/client64/orid.h
/usr/include/oracle/11.2/client64/orl.h
/usr/include/oracle/11.2/client64/oro.h
/usr/include/oracle/11.2/client64/ort.h
/usr/include/oracle/11.2/client64/xa.h
/usr/lib/oracle/11.2/client64/lib/libclntsh.so
/usr/lib/oracle/11.2/client64/lib/libocci.so
/usr/lib/oracle/11.2/client64/lib/ottclasses.zip
/usr/share/oracle/11.2/client64/cdemo81.c
/usr/share/oracle/11.2/client64/demo.mk
/usr/share/oracle/11.2/client64/occidemo.sql
/usr/share/oracle/11.2/client64/occidemod.sql
/usr/share/oracle/11.2/client64/occidml.cpp
/usr/share/oracle/11.2/client64/occiobj.cpp
/usr/share/oracle/11.2/client64/occiobj.typ
/usr/share/oracle/11.2/client64/ott
- 设置环境变量(/etc/profile)
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/oracle/11.2/client64/lib
export LD_LIBRARY_PATH
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
- 设置软连接
ln -s /usr/include/oracle/11.2/client64 $ORACLE_HOME/include
- 从源码安装cx_Oracle(cx_Oracle-5.3.tar.gz)
- 下载并解压cx_Oracle-5.3.tar.gz
- 进入cx_Oracle-5.3目录,执行
python3.4 setup.py install
或者python3.4 setup.py install --user
(这种用法下篇再讲)
[root@dev cx_Oracle-5.3]# python3.4 setup.py install
running install
running bdist_egg
running egg_info
writing cx_Oracle.egg-info/PKG-INFO
writing top-level names to cx_Oracle.egg-info/top_level.txt
writing dependency_links to cx_Oracle.egg-info/dependency_links.txt
reading manifest file 'cx_Oracle.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'cx_Oracle.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_ext
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
copying build/lib.linux-x86_64-3.4-11g/cx_Oracle.cpython-34m.so -> build/bdist.linux-x86_64/egg
creating stub loader for cx_Oracle.cpython-34m.so
byte-compiling build/bdist.linux-x86_64/egg/cx_Oracle.py to cx_Oracle.cpython-34.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying cx_Oracle.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying cx_Oracle.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying cx_Oracle.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying cx_Oracle.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
zip_safe flag not set; analyzing archive contents...
__pycache__.cx_Oracle.cpython-34: module references __file__
creating dist
creating 'dist/cx_Oracle-5.3-py3.4-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing cx_Oracle-5.3-py3.4-linux-x86_64.egg
creating /usr/local/lib/python3.4/site-packages/cx_Oracle-5.3-py3.4-linux-x86_64.egg
Extracting cx_Oracle-5.3-py3.4-linux-x86_64.egg to /usr/local/lib/python3.4/site-packages
Adding cx-Oracle 5.3 to easy-install.pth file
Installed /usr/local/lib/python3.4/site-packages/cx_Oracle-5.3-py3.4-linux-x86_64.egg
Processing dependencies for cx-Oracle==5.3
Finished processing dependencies for cx-Oracle==5.3
[root@dev cx_Oracle-5.3]#
使用import
查看发现是否已经安装成功
[root@localhost quant]$ python3.4
Python 3.4.4 (default, Jun 27 2017, 14:40:13)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>
网友评论