Chris Joo
Chris Joo

Reputation: 639

cx_Oracle connection by python3.5

I've tried several attempt to connect Oracle DB but still unable to connect. Following is my code to connect. However, I could connect Oracle DB through the terminal like this:
$ sqlplus64 uid/[email protected]:1521/WSVC

I wish your knowledge and experience associated with this issue to be shared. Thank you.

import os
os.chdir("/usr/lib/oracle/12.2/client64/lib")
import cx_Oracle  

# 1st attempt      
ip = '192.168.0.5'
port = 1521
SID = 'WSVC'
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
# dsn_tns = cx_Oracle.makedsn(ip, port, service_name=SID) 

db = cx_Oracle.connect('uid', 'passwd', dsn_tns)
cursor = db.cursor()

------------------------------------------------- 
# 2nd attempt  
conn = "uid/passwd@(DESCRIPTION=(SOURCE_ROUTE=OFF)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.5)(PORT=1521)))(CONNECT_DATA=(SID=WSVC)(SRVR=DEDICATED)))"

db = cx_Oracle.connect(conn)
cursor = db.cursor()

------------------------------------------------------ 
# ERROR Description 

cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle 

Upvotes: 1

Views: 969

Answers (1)

Anthony Tuininga
Anthony Tuininga

Reputation: 7086

The error "unable to acquire Oracle environment handle" is due to your Oracle configuration being incorrect. A few things that should help you uncover the source of the problem:

  • when using Instant Client, do NOT set the environment variable ORACLE_HOME; that should only be set when using a full Oracle Client or Oracle Database installation
  • the value of LD_LIBRARY_PATH should contain the path which contains libclntsh.so; the value you selected looks like it is incorrect and should be /usr/lib/oracle/12.2/client64/lib instead
  • you can verify which Oracle Client libraries are being loaded by using the ldd command as in ldd cx_Oracle.cpython-35m-x86_64-linux-gnu.so

Upvotes: 1

Related Questions