How to Install CX_Oracle for Python on Windows?

If you want to connect Oracle database from Python, then you need to install cx_Oracle package. In this tutorial, I am giving steps on how to download and install cx_Oracle package for Python on Windows.

Software Version Used

  • Python 3.7
  • cx_Oracle 6.4
  • OS Windows 10
  • Oracle Database 11g

Steps to Download and Install cx_Oracle Package for Python on Windows

  1. Click on the Download cx_Oracle link to download the package from Github.
  2. Extract the zip file to a folder on Windows. For example, F:\cx_oracle.
  3. Now open the command prompt and change the current directory to the F:\cx_oracle directory to install cx_Oracle package.
  4. Then run the following command.
python -m pip install cx_Oracle --upgrade pip

It will install the cx_Oracle package for Python on Windows, and you will get the messages as shown below.

Collecting pip
Downloading https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl (1.3MB)
100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 1.3MB 2.5MB/s
Installing collected packages: pip
Found existing installation: pip 10.0.1
Uninstalling pip-10.0.1:
Successfully uninstalled pip-10.0.1
Successfully installed pip-18.0

Now Test The Connection To Oracle From Python

Open Python IDE and give the following commands one by one to test the connection. Change your_username and your_psw to your database username and password.

  1. import cx_Oracle
  2. connection = cx_Oracle.connect("your_username", "your_psw", "localhost/orcl")
  3. cursor = connection.cursor()
  4. cursor.execute("""select to_char(sysdate, 'dd-mon-yyyy') from dual""")
  5. for cdate in cursor:
  6. print("Today the date is ", cdate)

Output

Today the date is ('05-sep-2018',)

See also:

Vinish Kapoor
Vinish Kapoor

Vinish Kapoor is a seasoned software development professional and a fervent enthusiast of artificial intelligence (AI). His impressive career spans over 20 years, marked by a relentless pursuit of innovation and excellence in the field of information technology. As an Oracle ACE, Vinish has distinguished himself as a leading expert in Oracle technologies, a title awarded to individuals who have demonstrated their deep commitment, leadership, and expertise in the Oracle community.

3 Comments

  1. Hi Vinish
    I ran into a "DatabaseError: ORA-12541: TNS:no listener" exception when trying to use the connect string here.
    The solution: the 'connection' must be extended by the port number defined in the corresponding listener setup (see listener.ora), e.g.:

    connection = cx_Oracle.connect("your_username", "your_psw", "localhost:port_nr/orcl")
    

    Kind regards,
    Stephan Borsodi (OCP)

    • it means listener service is not started. To start the listener issue the following command:

      lsnrctl start
      
    • Nope, this was not the root cause ...
      I am using an Oracle 19c (19.3.0.0) on Win10 and verifying the TNS infrastructure was the first thing I did:
      lsnrctl status listener_d193 # check the default listener with: lsnrctl status
      tnsping <TNS_servicename> # If the feedback is something like "OK (20 msec)", this means that the listener is reachable and running

      Since I am using a different port than the standard/default port 1521 in the listener configuration, I assume this was the cause.

Comments are closed.