Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection with Oracle cx_Oracle problem with AWS Glue Python Shell

I am working on AWS Glue Python Shell. I want to connect python shell with Oracle. I am successful installing psycopg2 and mysql libraries but when I tried to connect Oracle using cx_Oracle, I have successfully installed the library but I am facing the error

DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory"

I have tried following things

  1. I have downloaded so files from S3 and placed it in lib folder in parallel to the code file

  2. I have set the LD_LIBRARY_PATH, ORACLE_HOME using os.environ

I am using following code

import boto3
import os
import sys
import site
from setuptools.command import easy_install

s3 = boto3.client('s3')
dir_path = os.path.dirname(os.path.realpath(__file__))
#os.path.dirname(sys.modules['__main__'].__file__)

install_path = os.environ['GLUE_INSTALLATION']
easy_install.main( ["--install-dir", install_path, "cx_Oracle"] )

importlib.reload(site)

import cx_Oracle

conn_str = u'{username}/{password}@{host}:{port}/{sid}'
conn = cx_Oracle.connect(conn_str)
c = conn.cursor()
c.execute(u'select * from hr.countries')
for row in c:
    print(row[0], "-", row[1])
conn.close()
print('hello I am here');

I should be able to connect with oracle on aws glue python shell

like image 921
Nouman Khalid Avatar asked May 29 '26 07:05

Nouman Khalid


1 Answers

As it has already been mentioned in the responses. LD_LIBRARY_PATH needs to be set before the script starts. So, a way to avoid using LD_LIBRARY_PATH is setting rpath in the so file. Below are the steps needed.

You will need to update rpath in your so file. This can be done using patchelf package.

Please also include your libaio.so.1 in your so files which you might have generated by running sudo apt-get install libaio1

Installing patchelf

sudo apt-get update sudo apt-get install patchelf

To update rpath to your lib directory

patchelf --set-rpath <absolute_path_to_library_dir> libclntsh.so

Upload the so files with updated rpath to your glue env lib directory.

In your script you can then load the library.

from ctypes import *
cdll.LoadLibrary('<absolute_path_to_library_dir>/libclntsh.so')
like image 191
Harjeet Singh Avatar answered May 30 '26 21:05

Harjeet Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!