1

I am working on Python version 3. I want to connect Python Oracle Database (version Oracle 11g.). My code is below.

import cx_Oracle
conn = cx_Oracle.connect('PAYROLL/PAYROLL@localhost:1521/XE')
print(conn.version)

but it gives below error. Please help me to resolve this. I will be thankful.

Traceback (most recent call last):
  File "<string>", line 178, in run_nodebug
  File "D:\utils\python_files\connect oracle.py", line 1, in <module>
    import cx_Oracle
ModuleNotFoundError: No module named 'cx_Oracle'
6
  • What "install" documentation URL did you rely on? And then edit the question to paste in a console transcript of how the install went. Show us the output of $ python -m site. Do you find oracle files installed in one of those directories?
    – J_H
    Commented Aug 21, 2023 at 3:48
  • It means that the cx_Oracle module has not been installed. If you think you installed it, then it could be that there is more than one python on your system and you installed it in a different one.
    – tdelaney
    Commented Aug 21, 2023 at 4:22
  • it gives below error when i run below command C:\WINDOWS\system32>pip install cx_Oracle Collecting cx_Oracle note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for cx_Oracle Failed to build cx_Oracle ERROR: Could not build wheels for cx_Oracle, which is required to install pyproject.toml-based projects
    – Kamran
    Commented Aug 21, 2023 at 6:44
  • 1
    try installing oracledb instead, that's the new name and version for cx_Oracle, seeing your comment, not sure this module is still supported on 32 bit systems.
    – gsalem
    Commented Aug 21, 2023 at 7:30
  • but I have 64 bit OS
    – Kamran
    Commented Aug 21, 2023 at 8:02

1 Answer 1

2

The Python binary you are running doesn't have cx_Oracle installed.

Install the latest version of this module. It is now called python-oracledb, see the release announcement.

Follow the installation instructions. In summary:

python -m pip install oracledb

Since you want to connect to Oracle 11g, you will need to have Oracle Client libraries installed and you will need to add a call to init_oracle_client() (this was optional with cx_Oracle but required in what is called the 'Thick' mode of python-oracledb). From your connect string it seems your database is installed on your machine. It has the necessary Client Libraries available so you shouldn't need to install anything else. Assuming the libraries are in your PATH, then your code would be like:

import oracledb
oracledb.init_oracle_client()
conn = oracledb.connect('PAYROLL/PAYROLL@localhost:1521/XE')
print(conn.version)

Oracle DB 11 is very old and you should look at upgrading to something newer. If you have the XE edition then look at upgrading to Oracle 21c XE which is on https://www.oracle.com/database/technologies/xe-downloads.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.