About cx_Oracle

cx_Oracle is a module that enables access to Oracle Database that conforms with the Python database API specification. The module works with Oracle Database 11g and 12c and works for both Python 2.x and 3.x. This post covers the steps to install and set up cx_Oracle 5.3 in a Python 3.5 virtual environment on Oracle Linux 7.

Assumptions

  1. Oracle Linux 7 installed
    1. connected to the Internet
    2. configured for Oracle Linux yum server
  2. Oracle Database installed
    1. For these instructions, I used this git repo to set up install Oracle Database 12c Release 2 with Oracle VM VirtualBox and Vagrant. If you already have a database installation, adjust your paths, Oracle service name, and port accordingly.

Steps

  1. Enable required yum repositories
  2. Install Software Collections Library and Python 3.5
  3. Enable Python 3.5 utilities from the Software Collection Library in a bash shell
  4. Create a virtual environment and activate it
  5. Set up environment variables so that cx_Oracle can find Oracle Database installation and libraries
  6. Install cx_Oracle using pip
  7. Test cx_Oracle

 

Enable Required yum Repositories

Edit the yum.oracle.com configuration file and change enabled=0 to enabled=1 in the ol7_optional_latest and ol7_software_collections stanzas.

$ sudo vi /etc/yum.repos.d/public-yum-ol7.repo 
[ol7_optional_latest]
name=Oracle Linux $releasever Optional Latest ($basearch)
baseurl=http://yum.oracle.com/repo/OracleLinux/OL7/optional/latest/$basearch/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
gpgcheck=1
enabled=1
[ol7_software_collections]
name=Software Collection Library release 2.1 packages for Oracle Linux 7 (x86_64)
baseurl=http://yum.oracle.com/repo/OracleLinux/OL7/SoftwareCollections/x86_64/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
gpgcheck=1
enabled=1

Install Software Collections Library and Python 3.5

The software collection library enables you to install and use several different versions of the same software at the same time on a system. Software collections are great for development environments, which often require more recent versions of software components such as Perl, PHP, or Python to make use of the latest features, but which need to avoid the risk of disrupting other applications on the system that rely on different versions of these components. Now that the necessary yum repositories have been enabled to install the software collections library, run the following yum command:

$ sudo yum install gcc scl-utils rh-python35

Enable Python 3.5 utilities from the Software Collection Library in a bash shell

To enable the python 3.5 utilities —installed previously— in a bash shell

$ scl enable rh-python35 -- bash

Create a Python Virtual Environment

Python Virtual Environments help establish lightweight environments that are separate and isolated from the overall system's Python site directories.  From the Python documentation:

The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (allowing creation of environments with various Python versions) and can have its own independent set of installed Python packages in its site directories.

Use the Python venv module to create a virtual environment called my35env.

$ python -m venv my35env

Activate the virtual environment:

$ . my35env/bin/activate

 

Note how your bash prompt has changed to indicate you are now in a virtual environment:

(my35env) $

Set up environment variables so that cx_Oracle can find required libraries

Set the ORACLE_HOME and LD_LIBRARY_PATH environment variables. Remember to adjust the path below for your Oracle installation, if necessary.

(my35env) $ export ORACLE_HOME=/opt/oracle/product/12.2.0.1/dbhome_1
(my35env) $ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib

Install cx_Oracle using pip

Python 3.5 includes the pip module which makes installing cx_Oracle from PyPi, the Python Package Index straightforward.

(my35env) $ python -m pip install cx_Oracle

Look for the following output at the completion of the installation.

Successfully installed cx-Oracle-5.3

Test cx_Oracle

Assuming your database has service name orcl and a user scott/tiger, the following will verify whether cx_Oracle is working

(my35env) $ python
Python 2.7.5 (default, Sep 5 2016, 02:30:38)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> connection = cx_Oracle.Connection('scott', 'tiger', 'localhost:1521/orcl')
>>> connection.version
'12.2.0.1.0'

References