cx_Oracle module of python is used to connect to Oracle from Python. This works only with python 2.7. Here are the steps that can be used to install python2.7 on CentOS 6 and cx_Oracle module.
Installing python 2.7 as alternate python installation on CentOS 6wget "https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz" tar -zxvf Python-2.7.13.tgz cd Python-2.7.13 ./configure make make altinstall which python2.7 # To verify installation pathInstalling Oracle Instant Client
Download Oracle Instant Client from this link. We need instant client and instant client SDK. To install Oracle Instant client just unzip both the zip files to /opt or any other direcory
Create symbolic link libclntsh.so so it points to correct version.
cd /opt/instantclient_11_2/ ln -s libclntsh.so.11.1 libclntsh.so
Set environment variables
export ORACLE_HOME=/opt/instantclient_11_2 export LD_LIBRARY_PATH=/opt/instantclient_11_2Installing pip for python 2.7
pip is a package management system used to install python libraries. Here are the steps to install.
wget https://bootstrap.pypa.io/get-pip.py python2.7 get-pip.pyInstalling cx_Oracle module using pip
pip install cx_OracleSample program to test cx_Oracle
import cx_Oracle connection = cx_Oracle.connect ("username/password@hostname/service") cursor = connection.cursor () cursor.execute ("SELECT 1 a, 'AA' b, sysdate c FROM dual union select 2 a, 'BB' b, sysdate + 1 c from dual") result = cursor.fetchall () for row in result: print row[0], row[1], row[2] cursor.close () connection.close ()