linux Environmental use python Connect Oracle Need to use cx-Oracle and instantclient.
Internet environment direct
pip install cx_Oracle
Intranet environment
Go to Official website Upload and download wheel file , After importing the Intranet environment install
import cx_Oracle
conn = cx_Oracle.connect(rkcx_zhanghao/[email protected]:31521/rkqry)
Direct operation will report an error
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".
This is because the environment variables are not configured correctly , Lead to libclntsh.so This file was not found
Oracle Official website
Check Oracle Download the corresponding files according to the database version and system type !!!
my oracle yes 11.2.0 Version of the corresponding download package :
instantclient-basic-linux.x64-11.2.0.4.0.zip
linux The environment client needs to rely on libaio yum install libaio
Then create a new folder
mkdir -p /home/test/oracle
After the compressed package is moved , decompression
unzip instantclient-basic-linux.x64-11.2.0.4.0.zip
After decompression , Generate a newly generated folder instantclient_11_2
Add environment configuration , Under the user path ls -a see .bash_profile file .
The system is configured as
[[email protected]_191 ~]# vim /etc/.bash_profile
Only for the current user
[[email protected]_191 ~]# vim /home/test/.bash_profile
After opening the file
stay ~/.bash_profile Add the following at the end of the file
export LD_LIBRARY_PATH=/home/test/oracle/instantclient_11_2:$LD_LIBRARY_PATH
If python The output string is garbled or an error is reported , stay ~/.bash_profile Add
export LC_ALL='en_US.UTF-8'
After editing , Activate the configured file
[[email protected]_191 ~]source /home/test/.bash_profile
Then you can check whether the environment path just configured is correct
[[email protected]_191 ~]echo $LD_LIBRARY_PATH
Refer to the website