Kamran Agayev's Oracle Blog

Oracle Certified Master

Connect to Oracle from Python – write your first Python script!

Posted by Kamran Agayev A. on June 3rd, 2019

Python is getting more popular nowadays, because it is reliable and efficient, it has great corporate sponsors, and because of it’s amazing libraries that helps you to save time during the initial development cycle.

It’s much more easy to connect to an Oracle Database from Python by using cx_Oracle module. To get more information about cx_Oracle module, check the following links:




In this blog post, I will show how to install Python and configure the environment and connect to the database.

First of all, make sure you’ve an internet connection and install Python with yum as follows:

yum install python

After python is installed, install easy_install on Linux in order to download and manage Python packages easily using the following command:

wget http://bootstrap.pypa.io/ez_setup.py -O -| sudo python

easy_install installation

Next install pip using easy_install as follows:


Now install cx_Oracle module using pip as follows:



Now install Oracle instant client:

cd /etc/yum.repos.d
wget https://yum.oracle.com/public-yum-ol7.repo
yum install -y yum-utils
yum-config-manager --enable ol7_oracle_instantclient
yum list oracle-instantclient*



Now install Oracle instance client basic and sqlplus as follows:



After installing Oracle client, configure environment variables as follows:

vi .bashrc
export CLIENT_HOME=/usr/lib/oracle/18.3/client64


run .basrhc file to set environment variables and write your first Python script as follows:

vi connect.py 
import cx_Oracle
print con.version


If we run this script, we will get Oracle Database version in the output:

[root@oratest ~]python connect.py
[root@oratest ~]


Now let’s use split function in Python and split the version into “Version, Release and Patchset” sections as follows:

import cx_Oracle
print 'Version:', ver[0],'\nRelease:',ver[1],'\nPatchset:',ver[3]

[root@oratest ~]python connect.py
Version: 11
Release: 2
Patchste: 4
[root@oratest ~]


Now let’s create a table in Oracle and write a simple python code to query and print all rows in the table:

SQL> create table test_table(id number, name varchar2(10));
Table created.
SQL> insert into test_table values(1,'Oracle DB');
1 row created.
SQL> insert into test_table values(2,'SQL');
1 row created.
SQL> insert into test_table values(3,'PL/SQL');
1 row created.


Now create a python code to query the table:

import cx_Oracle
cur.execute('select * from test_table order by 1')
for result in cur:
      print result


[root@oratest ~]python connect.py
(1,'Oracle DB')
[root@oratest ~]

Congratulations! You’ve installed/configured Python, connected to an Oracle database, queried the table and printed the output!

3 Responses to “Connect to Oracle from Python – write your first Python script!”

  1. Shahnawaz Says:

    Thanks for KT .
    I would be happy if you can provide links where we can learn python and also get some hints to apply the skills in real time scenarios.


    Shahnawaz Moosa Ali Pagarkar

  2. Kamran Agayev A. Says:

    Hi Shahnawaz. Thank you for the comment!

    I will keep posting a new posts related with Python programming with Oracle. Keep tuned!

  3. Abhay Says:

    Nice post . Easy to understand. Thanks .

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>