cx_Oracle with multiple Oracle client versions
<p>I am running Python 2.7 and am using cx_Oracle on a linux 64 bit OS.  I need to be able to run against either an 11.2 or 12.1 Oracle client since I can't be sure which client will be installed on the deployed target.  I know there are cx_Oracle built against each Oracle client.  How can I be sure that the app will work?  I should mention I am using pyinstaller to package up the application.  Is there a version of cx_Oracle the is built against both Oracle clients or am I required to have two different versions of my application...one for 11g and one for a 12c client?</p>

<p>Although in theory you should be able to build an Oracle 11g version of cx_Oracle and use it with both an Oracle 11g and Oracle 12c client, I wouldn't recommend it. If you are able to convince your users to use the Oracle 12c instant client that would be the simplest. That client is able to access both 11g and 12c databases without any difficulty. But if that isn't possible, the other option is the following:</p>

<p>1) Compile cx_Oracle for both 11g and 12c and place both copies in your installation named (for example) cx_Oracle_11g.so and cx_Oracle_12c.so.</p>

<p>2) Import cx_Oracle dynamically using code like the following:</p>

<pre><code>for version in ("11g", "12c"):
    fileName = os.path.join(installDir, "cx_Oracle_%s.so" % version)
    try:
        module = imp.load_dynamic("cx_Oracle", fileName)
        break
    except ImportError:
        pass
</code></pre>

<p>3) Use the dynamically imported module wherever you need it in the same way as before. Since the dynamically loaded module was named cx_Oracle you should be able to import it in other code in the regular way and it will find the one you dynamically loaded.</p>

python python-2.7 cx-oracle