pythonoracle-databasepython-poetry

Does the oracle driver for python really require libraries from the native client?


I am writing some Python code and pulling in the

cx-oracle = "^8.3.0"

library with poetry. However, in order to make this work I actually have to initialize it with a directory where it can find the native libraries. Specifically, the Oracle documentation calls out that this needs the libraries for the quick client.

But, this is entirely non-portable. I want a project/bundle where when I type:

poetry install

Everything I need to run is installed into the virtual environment, an no extra configuration is required.

What I can do to get around this (and I don't want to) is actually put the libraries into a dedicated directory in source control and initialize it from there. But checking in binary dependencies into source has a very bad smell.

So, am I missing something, or do I really need to do this? I could see Oracle forcing this for licensing reasons.


Solution

  • Use python-oracledb instead - this doesn't necessarily need Oracle client libraries. cx_Oracle was obsoleted / renamed three years ago. See What is the difference between cx_Oracle and python-oracledb?

    The API is fundamentally the same in cx_Oracle and python-oracledb, since both support the Python DB API. The few differences are noted here.

    Installation doc is here. In summary:

    python3 -m pip install oracledb
    

    There is no need to install Oracle Client or Oracle Instant Client.

    Your code can simply be like:

    import getpass
    import oracledb
    
    un = 'cj'
    cs = 'localhost/orclpdb1'
    pw = getpass.getpass(f'Enter password for {un}@{cs}: ')
    
    with oracledb.connect(user=un, password=pw, dsn=cs) as connection:
        with connection.cursor() as cursor:
            sql = """select systimestamp from dual"""
            for (r,) in cursor.execute(sql):
                print(r)