Deploying cx_Oracle onto various versions of Oracl

2019-02-19 19:21发布

问题:

I have some small python apps that use cx_Oracle to connect to an Oracle database. I deploy these apps by compiling them with py2exe, which works fine in many cases.

The problem is, there is no standard Oracle Client version (9i and 10g for example) across the many people who need to install this, and it would be very frustrating to try to get everyone to standardize on a single Oracle Client version. I'm using the 9.2 client with cx_Oracle 4.4.1 for 9i at the moment, and so when I py2exe the resulting exe includes the cx_Oracle 4.4.1 library and will not work with 10g clients.

I don't use any specific features of any of the Oracle versions so there's really no reason for me to care what client version is being used, except for the cx_Oracle compatibility issues.

The ideal solution would be to somehow compile a version that is completely independent of the Oracle Client installed on the machine.

If that's not possible, I would be willing to compile separate exes for each major Oracle version (my_app_9i.exe, my_app_10g.exe, etc) but I can't figure out an easy way to even do this since installing a new cx_Oracle overwrites my old version, I would have to keep swapping the library back and forth to compile the other versions whenever I make a change.

Any advice or other options are welcome.

回答1:

If you want to build multiple cx_Oracle versions (eg: cx_Oracle10g, cx_Oracle11g, etc.) then you'll need to modify the cx_Oracle setup.py script. The last step in the script is a call to setup(); the first parameter is the name of the module to build. All you need to do is to change "cx_Oracle" to "cx_Oracle" + ver, where ver is 10g, 11g, etc. Either create several scripts and hard-code it, or add another parameter to setup.py to select it dynamically.

Of course, once you've got that, you need a mechanism to load the correct module at runtime. To do that you'll want to create your own cx_Oracle module that has a __init__.py file that looks something like this:

try:
  from cx_Oracle9g import *
except ImportError:
  try:
    from cx_Oracle10g import *
  except ImportError:
    try:
      from cx_Oracle11g import *

All you need to do is ship your custom cx_Oracle module plus the correct cx_OracleXg module with your application.

Alternately, you could have your custom cx_Oracle module dynamically check for each available Oracle client library (9g, 10g, 11g, etc) and then only import the correct matching cx_OracleXg module. In this case, you only have to ship a single binary, containing your custom cx_Oracle module plus all of the cx_OracleXg modules.