MacLochlainns Weblog

Michael McLaughlin's Technical Blog

Site Admin

Install cx_Oracle for Python

without comments

This shows you how to install the cx_Oracle library for Python 2.7 on Fedora Linux. If Fedora has it on the server you can download it with the following yum command:

yum install -y cx_Oracle-5.2.1-11g-py27-1.x86_64.rpm

Currently, you’ll get the following failure because it’s not available in the Fedora repository:

Loaded plugins: langpacks, refresh-packagekit
mysql-connectors-community                                      | 2.5 kB  00:00:00     
mysql-tools-community                                           | 2.5 kB  00:00:00     
mysql56-community                                               | 2.5 kB  00:00:00     
pgdg93                                                          | 3.6 kB  00:00:00     
updates/20/x86_64/metalink                                      | 2.3 kB  00:00:00     
No package cx_Oracle-5.2.1-11g-py27-1.x86_64.rpm available.
Error: Nothing to do

You can download the cx_Oracle library from the Python web site. The cx_Oracle documentation qualifies module interfaces, objects, and connections. Assuming your Linux user’s name is student, you download the cx_Oracle library into the /home/student/Downloads directory. Then, you use the su or sudo command to become the root user.

As the root user, run the following yum command:

yum install -y ~student/Downloads/cx_Oracle-5.2.1-11g-py27-1.x86_64.rpm

You should see the following output:

Loaded plugins: langpacks, refresh-packagekit
Examining /home/student/Downloads/cx_Oracle-5.2.1-11g-py27-1.x86_64.rpm: cx_Oracle-5.2.1-1.x86_64
Marking /home/student/Downloads/cx_Oracle-5.2.1-11g-py27-1.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package cx_Oracle.x86_64 0:5.2.1-1 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
=======================================================================================
 Package        Arch        Version      Repository                               Size
=======================================================================================
Installing:
 cx_Oracle      x86_64      5.2.1-1      /cx_Oracle-5.2.1-11g-py27-1.x86_64      717 k
 
Transaction Summary
=======================================================================================
Install  1 Package
 
Total size: 717 k
Installed size: 717 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction (shutdown inhibited)
  Installing : cx_Oracle-5.2.1-1.x86_64                                            1/1 
  Verifying  : cx_Oracle-5.2.1-1.x86_64                                            1/1 
 
Installed:
  cx_Oracle.x86_64 0:5.2.1-1                                                           
 
Complete!

After you install the cx_Oracle-5.2.1-1.x86_64 package, you can find the installed files with this rpm command:

rpm -ql cx_Oracle-5.2.1-1.x86_64

It lists:

/usr/lib64/python2.7/site-packages/cx_Oracle-5.2.1-py2.7.egg-info
/usr/lib64/python2.7/site-packages/cx_Oracle-5.2.1-py2.7.egg-info/PKG-INFO
/usr/lib64/python2.7/site-packages/cx_Oracle-5.2.1-py2.7.egg-info/SOURCES.txt
/usr/lib64/python2.7/site-packages/cx_Oracle-5.2.1-py2.7.egg-info/dependency_links.txt
/usr/lib64/python2.7/site-packages/cx_Oracle-5.2.1-py2.7.egg-info/top_level.txt
/usr/lib64/python2.7/site-packages/cx_Oracle.so
/usr/share/doc/cx_Oracle-5.2.1
/usr/share/doc/cx_Oracle-5.2.1/BUILD.txt
/usr/share/doc/cx_Oracle-5.2.1/README.txt
/usr/share/doc/cx_Oracle-5.2.1/samples
/usr/share/doc/cx_Oracle-5.2.1/samples/DatabaseChangeNotification.py
/usr/share/doc/cx_Oracle-5.2.1/samples/DatabaseShutdown.py
/usr/share/doc/cx_Oracle-5.2.1/samples/DatabaseStartup.py
/usr/share/doc/cx_Oracle-5.2.1/samples/ReturnLongs.py
/usr/share/doc/cx_Oracle-5.2.1/samples/ReturnUnicode.py
/usr/share/doc/cx_Oracle-5.2.1/samples/RowsAsInstance.py
/usr/share/doc/cx_Oracle-5.2.1/test
/usr/share/doc/cx_Oracle-5.2.1/test/3kArrayDMLBatchError.py
/usr/share/doc/cx_Oracle-5.2.1/test/3kNumberVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/3kStringVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/ArrayDMLBatchError.py
/usr/share/doc/cx_Oracle-5.2.1/test/BooleanVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/Connection.py
/usr/share/doc/cx_Oracle-5.2.1/test/Cursor.py
/usr/share/doc/cx_Oracle-5.2.1/test/CursorVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/DateTimeVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/IntervalVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/LobVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/LongVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/NCharVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/NumberVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/ObjectVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/SessionPool.py
/usr/share/doc/cx_Oracle-5.2.1/test/SetupTest.sql
/usr/share/doc/cx_Oracle-5.2.1/test/StringVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/TestEnv.py
/usr/share/doc/cx_Oracle-5.2.1/test/TimestampVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/test.py
/usr/share/doc/cx_Oracle-5.2.1/test/test3k.py
/usr/share/doc/cx_Oracle-5.2.1/test/test_dbapi20.py
/usr/share/doc/cx_Oracle-5.2.1/test/uArrayDMLBatchError.py
/usr/share/doc/cx_Oracle-5.2.1/test/uConnection.py
/usr/share/doc/cx_Oracle-5.2.1/test/uCursor.py
/usr/share/doc/cx_Oracle-5.2.1/test/uCursorVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/uDateTimeVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/uIntervalVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/uLobVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/uLongVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/uNumberVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/uObjectVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/uSessionPool.py
/usr/share/doc/cx_Oracle-5.2.1/test/uStringVar.py
/usr/share/doc/cx_Oracle-5.2.1/test/uTimestampVar.py

After you installed the software, you can test whether inside Python’s IDLE environment with the import command, like this:

Python 2.7.5 (default, Apr 10 2015, 08:09:05) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory

This error indicates that Oracle Client software isn’t installed, which is true in this case. I only installed the Oracle Database 11g Express Edition. You need to download the Oracle Client software and install it as the root user.

You download the Oracle Client software from the Oracle web site. Assuming your Linux user’s name is student, you download the cx_Oracle library into the /home/student/Downloads directory. Then, you use the su or sudo command to become the root user.

As the root user, run the following yum command:

yum install -y ~student/Downloads/oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm

You should see the following output:

Loaded plugins: langpacks, refresh-packagekit
Examining /home/student/Downloads/oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm: oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64
Marking /home/student/Downloads/oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package oracle-instantclient11.2-basic.x86_64 0:11.2.0.4.0-1 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
================================================================================
 Package
        Arch   Version
                      Repository                                           Size
================================================================================
Installing:
 oracle-instantclient11.2-basic
        x86_64 11.2.0.4.0-1
                      /oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64 179 M
 
Transaction Summary
================================================================================
Install  1 Package
 
Total size: 179 M
Installed size: 179 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction (shutdown inhibited)
  Installing : oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64           1/1 
  Verifying  : oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64           1/1 
 
Installed:
  oracle-instantclient11.2-basic.x86_64 0:11.2.0.4.0-1                          
 
Complete!

You can create a Python program that checks your ability to connect to the Oracle database, like the following oracleConnection.py file:

# Import the Oracle library.
import cx_Oracle
 
try:
  # Create a connection.
  db = cx_Oracle.connect("student/student@xe")
 
  # Print a message.
  print "Connected to the  Oracle " + db.version + " database."
 
except cx_Oracle.DatabaseError, e:
  error, = e.args
  print >> sys.stderr, "Oracle-Error-Code:", error.code
  print >> sys.stderr, "Oracle-Error-Message:", error.message
 
finally:
  # Close cursor. 
  db.close()

You can run this from the Linux command line with the following syntax:

python oracleConnection.py

It should return the following string:

Connected to the Oracle 11.2.0.2.0 database.

Now, you can create a Python program that reads data from the Oracle database. The following oracleString.py file reads a string literal from the pseudo table dual:

# Import the Oracle library.
import cx_Oracle
 
try:
  # Create a connection.
  db = cx_Oracle.connect("student/student@xe")
 
  # Create a cursor.
  cursor = db.cursor()
 
  # Execute a query.
  cursor.execute("SELECT 'Hello world!' FROM dual")
 
  # Read the contents of the cursor.
  for row in cursor:
    print (row[0]) 
 
except cx_Oracle.DatabaseError, e:
  error, = e.args
  print >> sys.stderr, "Oracle-Error-Code:", error.code
  print >> sys.stderr, "Oracle-Error-Message:", error.message
 
finally:
  # Close cursor and connection. 
  cursor.close()
  db.close()

You can run this from the Linux command line with the following syntax:

python oracleString.py

It should return the following string:

Hello world!

Now, you can create a Python program that reads actual table data from the Oracle database (assuming you have a copy of my video store database). The following oracleTable.py file reads a string literal from the pseudo table dual:

# Import the Oracle library.
import cx_Oracle
 
try:
  # Create a connection.
  db = cx_Oracle.connect("student/student@xe")
 
  # Create a cursor.
  cursor = db.cursor()
 
  # Execute a query.
  cursor.execute("SELECT item_title, item_subtitle FROM item")
 
  # Read the contents of the cursor.
  for row in cursor:
    print (row[0], row[1]) 
 
except cx_Oracle.DatabaseError, e:
  error, = e.args
  print >> sys.stderr, "Oracle-Error-Code:", error.code
  print >> sys.stderr, "Oracle-Error-Message:", error.message
 
finally:
  # Close cursor and connection. 
  cursor.close()
  db.close()

You can run this from the Linux command line with the following syntax:

python oracleTable.py

It should return the following strings (only a subset of the returned values):

("Harry Potter and the Sorcer's Stone", 'Two-Disc Special Edition')
('Harry Potter and the Chamber of Secrets', 'Two-Disc Special Edition')
('Harry Potter and the Prisoner of Azkaban', 'Two-Disc Special Edition')
('Harry Potter and the Chamber of Secrets', None)
('Harry Potter and the Goblet of Fire', 'Widescreen Edition')
('Harry Potter and the Goblet of Fire', 'Two-Disc Special Edition')
('Harry Potter and the Order of the Phoenix', 'Widescreen Edition')
('The Lord of the Rings - Fellowship of the Ring', 'Widescreen Edition')
('The Lord of the Rings - Fellowship of the Ring', 'Platinum Series Special Extended Edition')
('The Lord of the Rings - Two Towers', 'Widescreen Edition')
('The Lord of the Rings - Two Towers', 'Platinum Series Special Extended Edition')
('The Lord of the Rings - The Return of the King', 'Widescreen Edition')
('The Lord of the Rings - The Return of the King', 'Platinum Series Special Extended Edition')
('Star Wars - Episode I', 'The Phantom Menace')
('Star Wars - Episode II', 'Attack of the Clones')
('Star Wars - Episode III', 'Revenge of the Sith')
('Star Wars - Episode IV', 'A New Hope')
('Star Wars - Episode V', 'The Empire Strikes Back')
('Star Wars - Episode VI', 'Return of the Jedi')

As always, I hope this helps others who want to work with Python and the Oracle database.

Written by maclochlainn

October 19th, 2016 at 1:47 am