Forum Stats

  • 3,781,246 Users
  • 2,254,495 Discussions
  • 7,879,623 Comments

Discussions

Is the "Oracle Database for Python Developers Quickstart" in need of updating ?

I'm a new student user trying to setup a python scripted Oracle database so I apologize if my formatting or tech-speak is wrong. On the "Oracle Database for Python Developers, Part 1: cx_oracle quick start page.

They say that I should run the simplequery.py file but there is some formatting issues and I'm not sure what or how to fix it.

To start it adds expo infant of the import statement which causes an issue in python so I commented it out, then the bottom line starts with "rt" but I'm not aware of what that is.

# expo
import cx_Oracle
try:
   conn = cx_Oracle.connect("hr", "oracle", "dbhostname/orcl")
   sql="""SELECT d.* FROM departments d, locations l WHERE
       l.city = :city AND d.location_id = l.location_id"""
   column_length=[5, 22, 6, 5]
   cursor = conn.cursor()
   for row in cursor.execute(sql, city = 'Seattle'):
      for i in range (len(row)):
         print(str(row[i]).ljust(column_length[i]), end='')
      print()
except cx_Oracle.DatabaseError as exc:
   err, = exc.args
   print("Oracle-Error-Code:", err.code)
   print("Oracle-Error-Message:", err.message)
finally:
   cursor.close()
   conn.close()
   rt LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib:$LD_LIBRARY_PATH

Can anybody link me to a python script that works for python 3.8 or at least python 3. I'm getting better at learning python but now that I'm adding oracle database to it things get complicated quick.

Answers

  • user9540031
    user9540031 Member Posts: 144 Silver Badge

    Well... Looks like the "expo" bit has something to do with that "rt" thing down below 😉, yielding:

    export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib:$LD_LIBRARY_PATH
    

    which is obviously not Python, but totally Unix shell, and is arguably a prerequisite to have the LD_LIBRARY_PATH environment variable properly set up in order to have the OCI (Oracle Client Interface) library in the search path for shared libraries.

    My two cents... Hope this helps! (& Best of luck!)

    cormacoUser_EI9OM
  • @user9540031 So what you are saying is that I run the "export LD_LIBRARY..." from bash/linux to setup my PATH to accept oracle ?

  • user9540031
    user9540031 Member Posts: 144 Silver Badge

    Yes.

    LD_LIBRARY_PATH defines the search path for shared libraries—for details, see: man ld.so. Don't confuse it with PATH, which defines the search path for commands; both variables work the same way, but each serves its own purpose.

    AFAIK, the cx_Oracle Python module depends on the OCI library, therefore that library must be found in the search path.

    Don't forget to replace /usr/lib/oracle/12.2/client64/lib with the relevant path for your system: the Oracle client may be installed in a different place.

    Perhaps everything is already configured on your system: can you run sqlplus?

    $ sqlplus /nolog
    
    SQL*Plus: Release 19.0.0.0.0 - Production on Fri Dec 18 10:55:27 2020
    Version 19.5.0.0.0
    
    Copyright (c) 1982, 2019, Oracle.  All rights reserved.
    
    SQL> exit
    
    • If yes, you don't have anything more to do: the Oracle client is installed, and environment variables are properly set up.

    (And if you run the ldd command on the sqlplus binary you'll see that all dependencies are satisfied:

    $ which sqlplus
    
    /u01/app/oracle/product/19/bin/sqlplus
    
    $ ldd $(which sqlplus)
    
            linux-vdso.so.1 =>  (0x00007ffce2128000)
            libsqlplus.so => /u01/app/oracle/product/19/lib/libsqlplus.so (0x00007f0e1061c000)
            libclntsh.so.19.1 => /u01/app/oracle/product/19/lib/libclntsh.so.19.1 (0x00007f0e0c5f0000)
            libclntshcore.so.19.1 => /u01/app/oracle/product/19/lib/libclntshcore.so.19.1 (0x00007f0e0c050000)
            ...
            libc.so.6 => /lib64/libc.so.6 (0x00007f0e0a5db000)
            /lib64/ld-linux-x86-64.so.2 (0x00007f0e10908000)
    

    libclntsh.so and libclntshcore.so are the main components in the OCI library.)

    • If not: you need to find where the Oracle client is installed on your system, and set LD_LIBRARY_PATH accordingly (and export it so it is defined in child processes too).

    In case the Oracle client is not installed, or you can't find it, you may just download it from Oracle (here: https://www.oracle.com/database/technologies/instant-client/downloads.html). Most likely you'll need the Basic Package, SQL*Plus Package, and Tools Package: the OCI library comes with the Basic Package, but sooner or later you might need SQL*Plus / SQL*Loader to work with your database, so they're probably a useful addition.

    Remark: on the download page, you may see the following mention:

    Version 21.1.0.0.0 (Requires glibc 2.14)

    The release 2.14 of glibc is fairly old, so that requirement will be met on most systems, but it's worth checking anyway:

    $ rpm -qa | grep glibc
    
    ...
    glibc-2.17-292.0.1.el7.x86_64
    glibc-headers-2.17-292.0.1.el7.x86_64
    

    So it seems I have glibc release 2.17 on this box, which is fine 😊