Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect python to oracle

I have installed oracle client and oracle client-dev, and setup ORACLE_HOME environment variable. Yet when I attempt to install tux_oracle (python setup.py build) I get the following: fatal error: oci.h: No such file or directory

like image 350
kostas Avatar asked Nov 25 '11 08:11

kostas


3 Answers

Well, the answer is to install cx_Oracle not tx_Oracle. The way to do it in Ubuntu is the following:

  1. You must have the oracle client installed in your PC. If not download from oracle http://www.oracle.com/technetwork/topics/linuxsoft-082809.html.
  2. Install alien in your PC to be able to convert rpm to deb:
sudo apt-get -i alien
  1. Convert the rpm to deb:
sudo alien -d oracle-instantclient11.2-sqlplus-11.2.0.2.0.i386.rpm
  1. Install
    sudo dpkg -i oracle-instantclient11.2-basic_11.2.0.2.0-2_i386.deb
  2. Set ORACLE_HOME environment variable in /etc/profile.d by creating a file oracle.sh which must contain:
export ORACLE_HOME=/usr/lib/oracle/11.2/client
source oracle.sh
  1. Update LD_LIBRARY_PATH:
sudo vi /etc/ld.so.conf.d/oracle.conf which must contain:  
"$ORACLE_HOME/lib"
sudo ldconfig
  1. Download and install cx_Oracle (depending on the version of oracle and python you are using) from http://cx-oracle.sourceforge.net/ Again convert rpm to deb using alien and install:
sudo alien -d cx_Oracle-5.1-11g-py32-1.i386.rpm
sudo dpkg -i cx-oracle_5.1-2_i386.deb
  1. cx_Oracle gets installed in /usr/lib/python2.7/dist-packages/. You must do: sudo mv site-packages/cx_Oracle* dist-packages/ sudo rmdir site-packages/ sudo ln -s dist-packages site-packages

Now you should have no problem connecting to oracle. From python type:

import cx_Oracle

To connect to the database specify a connection string in tsnames.ora or directly:

connection_string = 'username/password@(DESCRIPTION= (ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=1521))(CONNECT_DATA= (SID=MY_SID)))'
like image 64
kostas Avatar answered Nov 03 '22 14:11

kostas


I am using cx_oracle to connect to my Oracle database. Did you give it a try?

like image 25
gecco Avatar answered Nov 03 '22 14:11

gecco


That header file can be found in the development suite. For some reason, it's not included in the default distribution package. Have a look at

http://my.opera.com/onyxluo/blog/cannot-find-oci-h-in-oracle9-2-client

Here's the page content, for your convenience. I got it from Google cache as the page was down when I got there.

The reason of this problem is because OCI (Oracle Call Interface)package is not installed in Oracle9.2 client. The default path of "oci.h" is $ORACLE_HOME/rdbms/demo. This problem doesn't exist on Oracle Database 9.2.0.1. But for Oracle 9.2.0.1 client, OCI package is not included in client even if you select the full package of client installation.

Solution:

  1. install Oracle 9.2.0.1 client first.
  2. In OUI(Oracle Universal Installer), use the same oracle home with Oracle 9.2.0.1 client, and then select Oracle Database install.
  3. Choose Customized in database installation
  4. Uncheck Enterprise Manager and Oracle Database and others except OCI and OCCI.

After OCI installed, $ORACLE_HOME/rdbms/demo will contain oci.h and other *.h files.

like image 29
thoaionline Avatar answered Nov 03 '22 16:11

thoaionline