Installing cx_Oracle in a virtualenv

For those of us who need our Python programs to talk to Oracle, cx_Oracle by Anthony Tuininga is an excellent choice. However, it has a binary installer that doesn’t let it install directly into a virtualenv.

But what if you needed to do this? Is it possible? The answer is, yes, but you have to jump through a couple of simple hoops first. Here’s how you can do it on Windows…

Overview

Assuming that you’ve already installed Python and have a virtualenv, the installation is 3 simple steps.

  1. Install an Oracle client.
  2. Install cx_Oracle into the global Python installation.
  3. Copy cx_Oracle into the virtualenv.

Install an Oracle client

First, you’ll need an Oracle client installation such as Oracle Instant Client. This can be downloaded and unzipped into a directory of your choice, as it does not have an installer. Take care to download the version of instant client that matches your version of Oracle. For example, if you have Oracle 12c then download instantclient-basic-nt-12.1.0.2.0.zip.

Important. If you’re deploying to an older OS such as Windows 2003 or Windows XP, then use the Oracle 11g instant client, as the Oracle 12c instant client uses WSAPoll() which doesn’t exist in older versions of Windows.

  • Unzip it to a directory of your choice, eg, C:\instantclient_12_1.

Install cx_Oracle into the global Python installation

Next, download the version of the cx_Oracle installer that matches your versions of Oracle Instant Client and Python. For example, if you have the Oracle 12c instant client and a 32-bit version of Python then download cx_Oracle-5.2.1-12c.win32-py3.5.exe.

  • Download the cx_Oracle installer that matches your Oracle client and Python versions.
  • Run the installer and install cx_Oracle into the global Python installation.

Copy cx_Oracle into the virtualenv

Copy the cx_Oracle files from your global Python installation’s site-packages/ directory into your virtualenv’s site-packages/. In this example we’ve been using Python 3.5 and cx_Oracle 5.2.1, so we would copy cx_Oracle-5.2.1.-py3.5.egg-info/ and cx_Oracle.pyd.

  • Copy the cx_Oracle files from the global Python to your virtualenv.

Verifying the installation

Finally, to verify the installation, add instant client to the PATH, activate your virtualenv, run python and import cx_Oracle.

  • Add instant client to the PATH.
C:> PATH=c:\instantclient_12_1;%PATH%
  • Activate your virtualenv.
  • Run python and import cx_Oracle.
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>>import cx_Oracle

It should import without errors. If you see DLL load failed then you probably forgot to add instant client to your PATH.

Advertisements

3 thoughts on “Installing cx_Oracle in a virtualenv

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s