I wrote the module to parse an Oracle version string into what we’d commonly expect to see, like the release number, an “R”, a release version, and then the full version number. The module name is more or less equivalent to a package name, and the file name is effectively the module name. The file name is strVersionOracle.py , which makes the strVersionOracle the module name.
# Parse and format Oracle version. def formatVersion(s): # Split string into collection. list = s.split(".") # Iterate through the result set. for i, l in enumerate(list): if i == 0 and list[i] == "11": label = str(l) + "g" elif i == 0 and list[i] == "12": label = label + str(l) + "c" elif i == 1: label = label + "R" + list[i] + " (" + s + ")" # Return the formatted string. return labelYou can put this in any directory as long as you add it to the python path. There are two Python paths to maintain. One is in the file system and the other is in Python’s interactive IDLE environment. You can check the contents of the IDLE path with the following interactive commands:
import sys print sys.path
It prints the following:
['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']You can append to the IDLE path using the following command:
sys.path.append("/home/student/Code/python")
After putting the module in the runtime path, you can test the code in the IDLE environment:
import cx_Oracle db = cx_Oracle.connect("student/student@xe") print strVersionOracle.formatVersion(db.version)
Line 3 prints the result by calling the formatVersion function inside the strVersionOracle module. It prints the following:
11gR2 (11.2.0.2.0)
You can test the program outside of the runtime environment with the following oracleConnection.py file. It runs
# Import the Oracle library. import cx_Oracle import strVersionOracle try: # Create a connection. db = cx_Oracle.connect("student/student@xe") # Print a message. print "Connected to the Oracle " + strVersionOracle.formatVersion(db.version) + " database." except cx_Oracle.Error, e: # Print the error. print "ERROR %d: %s" % (e.args[0], e.args[1]) finally: # Close connection. db.close()The oracleConnection.py program works when you call it from the Bash shell provided you do so from the same directory where the strVersionOracle.py file (or Python module) is located. If you call the oracleConnection.py file from a different directory, the reference to the library raises the following error:
Traceback (most recent call last): File "oracleConnection.py", line 3, in <module> import strVersionOracle ImportError: No module named strVersionOracle
You can fix this error by adding the directory where the strVersionOracle.py file exists, like
export set PYTHONPATH=/home/student/Code/python
Then, you can call successfully the oracleConnection.py file from any directory:
python oracleConnection.py
I hope this helps those trying to create and use Python modules.