Build a Python Application

To build a Python application that accesses an Autonomous AI Database, you start by configuring your development system to support database access that can take advantage of the continuous availability and high performance features of Autonomous AI Database.

After configuring your development system, you code database connections and SQL statements in your application to take advantage of these features.

Tip: For a “try it out” alternative to reading the following topics, you can go through the Lab 4: Build Python Application Stacks in Oracle Autonomous AI Database Dedicated for Developers and Database Users Workshop.

Configure Your Python Development System

To configure your development system so that your Python application can take advantage of the continuous availability and high performance features of an Autonomous AI Database, you perform these steps.

  1. Download and install Python.

  2. Download and install Oracle Instant Client.

  3. Download and install cx_Oracle.

  4. Download the client credentials for the database and make them available to Oracle Instant Client.

Before You Begin

Your development system must meet certain criteria to configure it successfully.

Download and Install Python

Download and Install Oracle Instant Client

You need Oracle Instant Client libraries version 19.6 or later.

Download and install the Oracle Instant Client basic package for your system’s OS and architecture:

Download and Install cx_Oracle

Use Python’s pip package to install cx_Oracle from PyPI (the Python Package Index).:

Download and Install Client Credentials for the Database

  1. Download the zip file containing client credentials for your database to a secure directory on your computer.

    This zip file is available for download from the database's Details page in the Oracle Cloud console. If you have an Oracle Cloud user account that permits you to access this page, download the credentials as follows. If you don't have such an account, you need to get the zip file from the administrator of the database, together with the password that was used to protect the zip file.

    1. In your web browser, sign in to Oracle Cloud and navigate to the Details page for the Autonomous AI Database.

    2. Click DB Connection.

    3. On the Database Connection page click Download.

    4. In the Download Wallet dialog, enter a wallet password in the Password field and confirm the password in the Confirm Password field.

      The password must be at least 8 characters long and must include at least 1 letter and either 1 numeric character or 1 special character.

    5. Click Download to save the client credentials zip file to a secure directory.

  2. After downloading the zip file, follow these steps:

    1. Unzip the client credentials zip file.

    2. Edit the sqlnet.ora file provided in the client credentials, replacing "?/network/admin" with the full path of the directory where you unzipped the client credentials; for example, change:

       (DIRECTORY="?/network/admin")
      

      to

       (DIRECTORY="/users/jdoe/adbcredentials")
      
    3. Create the TNS_ADMIN environment variable, setting its value to the full path of the directory where you unzipped the client credentials.

Code Database Connections and SQL Statements

After configuring your development system to support Python application connectivity to an Autonomous AI Database, follow these guidelines to achieve high performance and continuous availability of your application’s connections to the database:

For example:

pool = cx_Oracle.SessionPool("appuser",
                             SampleEnv.GetMainPassword(),
                             "tp_tls",
                             events=True,
                             threaded=True)

This example creates a pool for connections to the tp_tls database service, and enables FAN by setting the events parameter to True.

Related Content