Showing posts with label cx_oracle. Show all posts
Showing posts with label cx_oracle. Show all posts

Wednesday, September 21, 2011

Oracle instant client and cx_Oracle on OS X Lion

In case you are doing Python development on a Mac and connecting to an Oracle database, there's a good chance that you've already run into the segfault (Segmentation fault: 11) screen. First, I thought it was something to do with the cx_Oracle that I had just updated but it turns out that the 64-bit version of the Oracle instant client is busted on the OS X Lion platform.

Only way around this is to use the 32-bit version of the instant client instead. The way you do this is that you download and install the 32-bit instant client (basic-lite and sdk) from Oracle, use Python in 32 bit mode and install cx_Oracle.

  1. instant client comes with pretty decent installation instructions, so just follow them (set three env vars and create the symlink)
  2. to run python in 32 bit mode you have two options (this is all explained in Python's man page):
    1. % defaults write com.apple.versioner.python Prefer-32-Bit -bool yes
    2. % export VERSIONER_PYTHON_PREFER_32_BIT=yes
  3. remove the old cx_Oracle by simply removing the .egg under /Library/Python/2.7/site-packages/. So, for example: % sudo rm /Library/Python/2.7/site-packages/cx_Oracle-5.1-py2.7-macosx-10.7-intel.egg
  4. lastly say: sudo -E easy_install cx_Oracle

These instructions assume you're installing cx_Oracle globally to your system, hence sudo. I actually tried going through this process in a virtualenv but it didn't work. However, I only tried once so maybe I missed something.. and I had to do a global install anyway so I haven't bothered with the virtualenv for now. Will try that again later.

(In case you run into problems, you might want to reboot after the installation because I think OS X leaves some libraries into memory and when you switch back and forth with different versions of libraries, you may actually end up using a different library than what you think.)

Wednesday, April 13, 2011

Using oracle instant_client from Python on Ubuntu

I do a lot of Python development in different environments. Ubuntu is one of them. Oracle is not the easiest beast to deal with but the instant_client and cx_Oracle make it tolerable. The way I usually set this up is to do following:


  1. download instant_client basic lite and instant client sdk from oracle
  2. unzip both into same dir (e.g. /home/jedi/instant_client)
  3. export ORACLE_HOME=/home/jedi/instant_client
  4. export LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
  5. cd $ORACLE_HOME
  6. ln -s libclntsh.so.11.1 libclntsh.so  (replace with your version number)
  7. sudo apt-get install python-dev python-setuptools libaio-dev
  8. sudo -E easy_install cx_Oracle
This should do it. You can test it with:

$> python

>>> import cx_Oracle
>>> conn = cx_Oracle.connect("username/password@//example.host.com/orasid")
>>> crsr = conn.cursor()
>>> crsr.execute("select * from mytable")
>>> for row in crsr: print repr(row)  # hit enter twice
...

>>> crsr.close()
>>> conn.close()
>>> quit()

Credits where credit's due:  Catherine's howto for XE