Home » Python » How to Install CX_Oracle for Python on Windows?

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: