cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: “ cannot open shared object file: No such file or directory”

cx_Oracle.DatabaseError DPI-1047
cx_Oracle.DatabaseError DPI-1047

I have Python script which inserts certain data to an Oracle Autonomous Database. I order to connect to the Oracle Database, I am using cx_Oracle module. When I run this script directly, there is no problem. However, when I tried to run a crontab it threw this error:

A similar question is asked here. The accepted answer did not work for me but one of the comments in one of the answers helped me to solve this problem.

1. Permanently add Instant Client to the runtime link path:

For this just use ldconfig .

sudo sh -c "echo /opt/oracle/instantclient_21_4 > /etc/"
sudo ldconfig

2. Set the environment variables in a shell script and then invoking Python from that script:

#create a file to contain shell script:
#place the following code to the top of the file:
#! /bin/sh
#Set the environment variables and write your Python code here and save this file.
this is the file
this is the file
#! /bin/sh

export PATH="$PATH:/opt/oracle/instantclient_21_4"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/opt/oracle/instantclient_21_4"
sudo /usr/bin/python3 /root/binance_hist/

3. Set the crontab:

0 * * * * bash /root/binance_hist/ >> /root/binance_hist/hourly_alert.log 2>&1

So, instead of calling the Python script directly from crontab, we create a shell script, call the Python from shell script along with necessary environment variables and then set a cronjob so that it runs the shell script. That solves the problem.