Ian Nato
Ian Nato

Reputation: 1037

How to fix: cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library - Python

I am establishing a connection to oracle 11g which is in a remote server using cx_oracle 7 with python 3.6.7. my OS in Ubuntu 18.04

I have installed oracle instant client library with libclntsh.so but I am not getting the expected output.

here is the code which i am using to connect to the oracle db

connection = cx_Oracle.connect("username/password@host/port")
print (connection.version)
connection.close()

when the script runs i expect to get the connection version instead i am getting the following error message

File "script.py", line 13, in connection = cx_Oracle.connect("username/password@host/port") cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory". See https://oracle.github.io/odpi/doc/installation.html#linux for help

Upvotes: 38

Views: 155351

Answers (11)

Adnan Baig
Adnan Baig

Reputation: 1

I was facing the same issue DPI:1047 when i was running my application using docker, In local application was running fine.

I changed my code from this

dsn = cx_Oracle.makedsn("server", 1521, sid="SID")
cx_Oracle.connect(
            user="username,
            password="password",
            dsn=dsn,
            encoding="UTF-8"
        )

To this used oracledb instead of cx_Oracle because oracledb is newest version of cx_Oracle that dont require Oracle Client Library.

dsn = orackledb.makedsn("server", 1521, sid="SID")
oracledb.connect(
            user="username,
            password="password",
            dsn=dsn
        )

I also did changes in docker file

RUN wget https://download.oracle.com/otn_software/linux/instantclient/instantclient-basiclite-linuxx64.zip && \
    unzip instantclient-basiclite-linuxx64.zip && \
    rm -f instantclient-basiclite-linuxx64.zip && \
    cd instantclient* && \
    rm -f *jdbc* *occi* *mysql* *jar uidrvci genezi adrci && \
    echo /opt/oracle/instantclient* > /etc/ld.so.conf.d/oracle-instantclient.conf && \
    ldconfig

Upvotes: 0

Todo
Todo

Reputation: 1

I had the same problem and none of the solutions above worked (On fedora 39). So I give you my procedure for Oracle 11.2 (package dll on https://www.oracle.com/in/database/technologies/instant-client/linux-x86-64-downloads.html) :

dnf install -y libnsl

cd /tmp #I put the package in /tmp

dnf install -y oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm 

cd /usr/lib/oracle/11.2/client64/lib/

ln -s libclntsh.so.11.1 libclntsh.so #the point that stuck me for a long time

echo "/usr/lib/oracle/11.2/client64/lib/" > /etc/ld.so.conf.d/oracle.conf

ldconfig

You had to be root to run most of these commands.

Upvotes: 0

Ruwan Pathirana
Ruwan Pathirana

Reputation: 157

Download oracle instant client from here

Then extract file and copy the local path,

import cx_Oracle
cx_Oracle.init_oracle_client(lib_dir=r"C:/instantclient_21_13")   #replace your path

Upvotes: 0

Eli Saul Florez
Eli Saul Florez

Reputation: 1

sudo su
mkdir -p /opt/oracle
cd /opt/oracle
wget https://download.oracle.com/otn_software/linux/instantclient/19800/instantclient-basic-linux.x64-19.8.0.0.0dbru.zip
unzip instantclient-basic-linux.x64-19.8.0.0.0dbru.zip
apt install libaio1
echo /opt/oracle/instantclient_19_8 > /etc/ld.so.conf.d/oracle-instantclient.conf
ldconfig
pip install cx_Oracle
exit

Upvotes: -1

Hạ Huy
Hạ Huy

Reputation: 11

mkdir -p /opt/oracle
cd /opt/oracle
wget https://download.oracle.com/otn_software/linux/instantclient/19800/instantclient-basic-linux.x64-19.8.0.0.0dbru.zip
unzip instantclient-basic-linux.x64-19.8.0.0.0dbru.zip
apt install libaio1
echo /opt/oracle/instantclient_19_8 > /etc/ld.so.conf.d/oracle- instantclient.conf
ldconfig
pip install cx_Oracle

It work for me after search in google 2 day and try more 10 solution

Upvotes: 1

Ravikumar Kumarasamy
Ravikumar Kumarasamy

Reputation: 59

I was facing similar issue with Debian 10 image version with python, I had followed below steps to resolve my docker image issue:

Step1 : downloaded oracle-instantclient19.19-basic-19.19.0.0.0-1.x86_64.rpm from Oracle official site:

Step 2: Installed in the base image

Step 3: I had setup following ENV variables:

ENV ORACLE_HOME=/usr/lib/oracle/19.19/client64 ENV LD_LIBRARY_PATH=$ORACLE_HOME/lib

This resolved my issue

Upvotes: 0

dhina karan
dhina karan

Reputation: 221

I have almost given up on this error. I tried potentially all the solution on the internet and nothing worked.

I was writing a similar script in Unix where I was getting the same error. I have applied the same fix that I used in shell script and it worked like charm.

The Python script worked fine accessing DB via cx_Oracle if I execute it directly. But when I schedule it through crontab I was constantly getting the error "cx_Oracle.DatabaseError: DPI-1047"

Here is the fix. Import the OS module and add the below code.

os.environ["ORACLE_HOME"] = "Your oracle lib ". In my case it is /u01/oracle/product/12.1.0.2/

Upvotes: 2

Ian Nato
Ian Nato

Reputation: 1037

After some more research i got the solution from Ubuntu community , after you have installed oracle instant-client you will have to integrate oracle libraries as follows:

export LD_LIBRARY_PATH=/usr/lib/oracle/<version>/client(64)/lib/${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}

An example for 12.1 version for Linux x86_64 can be:

export LD_LIBRARY_PATH=/usr/lib/oracle/12.1/client64/lib/${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}

where <version> indicates the version of your of your oracle instant-client e.g 11.2, 12.2
The connection parameter should be as follows

connection = cx_Oracle.connect("username/password@host/service_name e.g orcl")

to get the listener/service_name type the following in the oracle sqlplus

SQL> show parameter local_listener

literal under VALUE is your listener/service_name.

Upvotes: 29

Leigh Mathieson
Leigh Mathieson

Reputation: 2038

For Ubuntu Linux 20.04 LTS server what worked for me (which may be obvious but wasn't to me!) is 1) when you perform the export, you need to be in the folder you intend to run the app/ command connecting to Oracle from, and although this worked, after closing the SSH terminal to the EC2 server, was then not available again which was resolved by 2) Add it to ~/.bashrc Steps in full:

With the Oracle instant client unzipped in for example: /opt/oracle/instantclient_19_9

sudo apt-get install libaio1
cd ~/your-project-folder
export LD_LIBRARY_PATH=/opt/oracle/instantclient_19_9

I then added to ~/.bashrc with:

sudo nano ~/.bashrc

And add this line:

export LD_LIBRARY_PATH=/opt/oracle/instantclient_19_9

And in the terminal run:

source ~/.bashrc

Mine worked as expected installed on an EC2 server under 'ubuntu' user with requisite nvm/ nodeJs installed

In nodeJs an example connection might look something like:

const testOracleConnection = async () => {
    let conn;

    try {
        conn = await oracledb.getConnection(oracleConfig);

        const query1 = 'select ID, anotherColumn from someTable where ID = 1111';

        const result = await conn.execute(query1);

        console.log(result);
    } catch (err) {
        console.error(err);
    } finally {
        if (conn) {
            try {
                await conn.close();
            } catch (err) {
                console.error(err);
            }
        }
    }
};

Upvotes: 7

Wilber Hernandez
Wilber Hernandez

Reputation: 1

If you're working with aws lambdas to connect to your RDS/OracleDB, then try this approach using Docker to automated the build for the aws lambda layer - https://medium.com/@sabithvm/building-up-on-lambda-layers-a4771d3b9c7

Upvotes: -2

Michel Mesquita
Michel Mesquita

Reputation: 783

I was facing the exact same problem. This is what worked for me:

  • First, I downloaded the Oracle Basic zip file. In my case, I got the 64-bit version.
  • After that, I unzipped it in an opt directory. I had to use sudo in my system
    $ sudo mkdir -p /opt/oracle  

    $ cd /opt/oracle  

    $ sudo unzip /opt/oracle/instantclient-basic-linux.x64-19.8.0.0.0dbru.zip  
  • Then I installed libaio1. Note that I am working with Ubuntu
    $ sudo apt-get install libaio1
  • Finally, I added the path to the external variable LD_LIBRARY_PATH
    $ vim ~/.bashrc  
  • And added this line to the .bashrc file
    export LD_LIBRARY_PATH=/opt/oracle/instantclient_19_8:$LD_LIBRARY_PATH  
  • After saving the .bashrc file, I sourced it:
    $ source ~/.bashrc

Then my Python script worked nicely again.

See also the cx_oracle documentation

Upvotes: 21

Related Questions