[python] install cx_oracle for python

Am on Debian 5, I've been trying to install cx_oracle module for python without any success. First, I installed oracle-xe-client and its dependency (followed tutorial in the following link here).

Then, I used the scripts in /usr/lib/oracle/xe/app/oracle/product/10.2.0/client/bin to populate environment variables such as PATH, ORACLE_HOME and NLS_LANG.

Once, this was completed, I tried to run:

sudo easy_install cx_oracle

But I keep getting the following error:

Searching for cx-oracle
Reading http://pypi.python.org/simple/cx_oracle/
Reading http://cx-oracle.sourceforge.net
Reading http://starship.python.net/crew/atuining
Best match: cx-Oracle 5.0.4
Downloading http://prdownloads.sourceforge.net/cx-oracle/cx_Oracle-5.0.4.tar.gz?download
Processing cx_Oracle-5.0.4.tar.gz
Running cx_Oracle-5.0.4/setup.py -q bdist_egg --dist-dir /tmp/easy_install-xsylvG/cx_Oracle-5.0.4/egg-dist-tmp-8KoqIx
error: cannot locate an Oracle software installation

Any idea what I missed here?

This question is related to python oracle

The answer is


Alternatively you can install the cx_Oracle module without the PIP using the following steps

  1. Download the source from here https://pypi.python.org/pypi/cx_Oracle [cx_Oracle-6.1.tar.gz ]
  2. Extract the tar using the following commands (Linux)

    gunzip cx_Oracle-6.1.tar.gz

    tar -xf cx_Oracle-6.1.tar

  3. cd cx_Oracle-6.1

  4. Build the module

    python setup.py build

  5. Install the module

    python setup.py install


This worked for me

python -m pip install cx_Oracle --upgrade

For details refer to the oracle quick start guide

https://cx-oracle.readthedocs.io/en/latest/installation.html#quick-start-cx-oracle-installation


Thx Burhan Khalid, I overlooked your "You need to be root" quote, but found the way when you are not the root here.

At point 7 you need to use:

sudo env ORACLE_HOME=$ORACLE_HOME python setup.py install 

Or

sudo env ORACLE_HOME=/path/to/instantclient python setup.py install

Try to reinstall it with the following code:

!pip install --proxy http://username:[email protected]:8080 --upgrade --force-reinstall cx_Oracle

Thanks Burhan Khalid. Your advice to make a a soft link make my installation finally work.

To recap:

  1. You need both the basic version and the SDK version of instant client

  2. You need to set both LD_LIBRARY_PATH and ORACLE_HOME

  3. You need to create a soft link (ln -s libclntsh.so.12.1 libclntsh.so in my case)

None of this is documented anywhere, which is quite unbelievable and quite frustrating. I spent over 3 hours yesterday with failed builds because I didn't know to create a soft link.


I think it may be the sudo has no access to get ORACLE_HOME.You can do like this.

sudo visudo

modify the text add

Defaults env_keep += "ORACLE_HOME"

then

sudo python setup.py build install


The alternate way, that doesn't require RPMs. You need to be root.

  1. Dependencies

    Install the following packages:

    apt-get install python-dev build-essential libaio1
    
  2. Download Instant Client for Linux x86-64

    Download the following files from Oracle's download site:

    files preview

  3. Extract the zip files

    Unzip the downloaded zip files to some directory, I'm using:

    /opt/ora/
    
  4. Add environment variables

    Create a file in /etc/profile.d/oracle.sh that includes

    export ORACLE_HOME=/opt/ora/instantclient_11_2
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME
    

    Create a file in /etc/ld.so.conf.d/oracle.conf that includes

    /opt/ora/instantclient_11_2
    

    Execute the following command

    sudo ldconfig
    

    Note: you may need to reboot to apply settings

  5. Create a symlink

    cd $ORACLE_HOME 
    ln -s libclntsh.so.11.1 libclntsh.so
    
  6. Install cx_Oracle python package

    • You may install using pip

      pip install cx_Oracle
      
    • Or install manually

      Download the cx_Oracle source zip that corresponds with your Python and Oracle version. Then expand the archive, and run from the extracted directory:

      python setup.py build 
      python setup.py install
      

This just worked for me on Ubuntu 16:

Download ('instantclient-basic-linux.x64-12.2.0.1.0.zip' and 'instantclient-sdk-linux.x64-12.2.0.1.0.zip') from Oracle web site and then do following script (you can do piece by piece and I did as a ROOT):

apt-get install -y python-dev build-essential libaio1
mkdir -p /opt/ora/
cd /opt/ora/

## Now put 2 ZIP files:
# ('instantclient-basic-linux.x64-12.2.0.1.0.zip' and 'instantclient-sdk-linux.x64-12.2.0.1.0.zip') 
# into /opt/ora/ and unzip them -> both will be unzipped into 1 directory: /opt/ora/instantclient_12_2 

rm -rf /etc/profile.d/oracle.sh
echo "export ORACLE_HOME=/opt/ora/instantclient_12_2"  >> /etc/profile.d/oracle.sh
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME"  >> /etc/profile.d/oracle.sh
chmod 777 /etc/profile.d/oracle.sh
source /etc/profile.d/oracle.sh
env | grep -i ora  # This will check current ENVIRONMENT settings for Oracle


rm -rf /etc/ld.so.conf.d/oracle.conf
echo "/opt/ora/instantclient_12_2" >> /etc/ld.so.conf.d/oracle.conf
ldconfig
cd $ORACLE_HOME  
ls -lrth libclntsh*   # This will show which version of 'libclntsh' you have... --> needed for following line:

ln -s libclntsh.so.12.1 libclntsh.so

pip install cx_Oracle   # Maybe not needed but I did it anyway (only pip install cx_Oracle without above steps did not work for me...)

Your python scripts are now ready to use 'cx_Oracle'... Enjoy!


If you are trying to install in MAC , just unzip the Oracle client which you downloaded and place it into the folder where you written python scripts. it will start working.

There is too much problem of setting up environmental variables. It worked for me.

Hope this helps.

Thanks


The following worked for me, both on mac and Linux. This one command should download needed additional files, without need need to set environment variables.

python -m pip install cx_Oracle --pre

Note, the --pre option is for development and pre-release of the Oracle driver. As of this posting, it was grabbing cx_Oracle-6.0rc1.tar.gz, which was needed. (I'm using python 3.6)