Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cx_oracle and python 2.7 [duplicate]

Im using python 2.7 and cx_oracle ( Windows x86 Installer (Oracle 10g, Python 2.7) ) and 'm having a bad time to set this simple example bellow to work:

import cx_Oracle
connection = cx_Oracle.connect('user/pass@someserver:port')
cursor = connection.cursor()
cursor.execute('select sysdate from dual')

for row in cursor:
    print row
connection.close()

Error Message:

Traceback (most recent call last):
  File "C:\ORACON.py", line 1, in <module>
    import cx_Oracle
ImportError: DLL load failed: The specified module could not be found.

For now, what i have done was:

1) installed the cx_oracle binary;

2) downloaded instantclient_10_2 from oracle website and exported the path to environment;

Anyone know what im missing?

Thank you for your time on reading this.

like image 386
thclpr Avatar asked Dec 04 '12 17:12

thclpr


People also ask

Does cx_Oracle require Oracle client?

cx_Oracle requires Oracle Client libraries. The libraries provide the necessary network connectivity to access an Oracle Database instance.

What is Python cx_Oracle?

cx_Oracle is a module that enables access to Oracle Database and conforms to the Python database API specification. This module is currently tested against Oracle Client 21c, 19c, 18c, 12c, and 11.2, and Python 3.6, 3.7, 3.8, 3.9 and 3.10. Older versions of cx_Oracle may be used with previous Python releases.

Can I connect Python with Oracle?

Quick Start: Developing Python Applications for Oracle Database. This tutorial shows you how to connect Python applications to Oracle Database using the cx_Oracle interface. This interface lets you quickly develop applications that execute SQL or PL/SQL statements.

What is import cx_Oracle?

cx_Oracle is a Python extension module that enables access to Oracle Database. It conforms to the Python database API 2.0 specification with a considerable number of additions and a couple of exclusions.


1 Answers

I was able to solve this problem with the following steps:

  1. Download instantclient-basic-win32-10.2.0.5 from Oracle Website

  2. unzipped the into my c:\ with the name oraclient

  3. Created the directory structure C:\oraclient\network\admin to add the TNSNAMES.ORA

  4. Added the TNS_ADMIN env var pointing to C:\oraclient\network\admin

  5. Added the ORACLE_HOME env var pointing to C:\oraclient\

After that i used the following code:

import cx_Oracle

con = cx_Oracle.connect('theuser', 'thepass', 'your DB alias on your TNSNAMES.ORA file ')
cur = con.cursor()
if cur.execute('select * from dual'):
    print "finally, it works!!!"
else:
    print "facepalm"
con.close()

I hope it helps someone.

like image 169
thclpr Avatar answered Oct 30 '22 19:10

thclpr