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.
- Oracle Linux 7 installed
- connected to the Internet
- configured for Oracle Linux yum server
- Oracle Database installed
- 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.
- Enable required yum repositories
- Install Software Collections Library and Python 3.5
- Enable Python 3.5 utilities from the Software Collection Library in a bash shell
- Create a virtual environment and activate it
- Set up environment variables so that cx_Oracle can find Oracle Database installation and libraries
- Install cx_Oracle using pip
- 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
name=Oracle Linux $releasever Optional Latest ($basearch)
name=Software Collection Library release 2.1 packages for Oracle Linux 7 (x86_64)
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:
venvmodule 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:
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/220.127.116.11/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
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')