oracle / python-cx_Oracle

Python interface to Oracle Database now superseded by python-oracledb
https://oracle.github.io/python-cx_Oracle
Other
889 stars 361 forks source link

module 'cx_Oracle' has no attribute 'connect' #614

Closed gnsallman closed 2 years ago

gnsallman commented 2 years ago

Python version: 3.8 Running on AWS Lambda cx_Oracle version installed via pip: 8.3.0 Oracle DB version: 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production Oracle Instant Client Basic Light Package downloaded: instantclient-basiclite-linux.x64-18.5.0.0.0dbru.zip

Code block:

import cx_Oracle

conn = cx_Oracle.connect(user=user, password=password, dsn=host)
cursor = connection.cursor()
cursor.execute(query)

The error I get is: [ERROR] AttributeError: module 'cx_Oracle' has no attribute 'connect'

I was thinking it was a problem with the client libraries not being found, so I added

lib_dir = os.path.join(os.environ["LAMBDA_TASK_ROOT"], "lib")
cx_Oracle.init_oracle_client(lib_dir=lib_dir)

but then I got: [ERROR] AttributeError: module 'cx_Oracle' has no attribute 'init_oracle_client'

I also tried downgrading cx_Oracle to 8.0.0 but still the same errors.

All I have in my lambda zip for cx_Oracle is the cx_Oracle.cpython-36m-x86_64-linux-gnu.so binary itself. Do I need anything else besides the client library?

sharadraju commented 2 years ago

Hello @gnsallman, If you are using Linux, then the init_oracle_client()function will not load the client libraries. You will have to load the client libraries externally through ldconfig utility or set the LD_LIBRARY_PATH environment variable to the directories of the instant client libraries. If you want to avoid this, On Linux, you can alternatively install cx_Oracle and Instant Client RPM packages from yum.oracle.com, see yum.oracle.com/oracle-linux-python.html

Please check cx_Oracle installation documentation for Linux on cx-oracle.readthedocs.io/en/latest/user_guide/installation.html?highlight=linux#installing-cx-oracle-on-linux

anthony-tuininga commented 2 years ago

That error suggests that you are not getting cx_Oracle but some shim that is masquerading as cx_Oracle. You can find out by printing the location of the module and examining it, like this:

import cx_Oracle
print(cx_Oracle)
gnsallman commented 2 years ago

I added

import inspect

print(cx_Oracle)
print("getfile:")
inspect.getfile(cx_Oracle)

and I get

<module 'cx_Oracle' (namespace)>
getfile:
[ERROR] TypeError: <module 'cx_Oracle' (namespace)> is a built-in module

which doesn't make sense. I can import the module in my local python interpreter fine so this is something with Lambda

cjbj commented 2 years ago

I recall seeing some StackOverflow posts about configuring Instant Client on lambda. Check them out; they might also help you with Python.

I'll close this issue since installing on AWS is out of our knowledge area, but if you find a solution, feel free it note it here.