Python QuickStart

time to complete
10 minutes

InterSystems IRIS® data platform supports two lightweight Python APIs that provide direct access to InterSystems IRIS databases via relational tables or multidimensional storage:

  • PyODBC lets your application quickly retrieve, update, and delete data.
  • The Native API for Python lets your application directly access the underlying data structure within InterSystems IRIS, known as globals, as well as call ObjectScript methods and routines.

Watch the video to review ways to connect your application to InterSystems IRIS data platform, or follow the steps in the exercise below to use PyODBC and the Native API for Python to connect to InterSystems IRIS.

note
Note:
Python is available on InterSystems IRIS versions 2019.2 and newer.

In the steps below, you will access a set of sample stock data using each of the ways described. With Python, you can interact with InterSystems IRIS relationally (with PyODBC) or natively (with the Native API). Note: The sample code and supporting wheel files only support Python 3.

Set up


  1. First, make sure you have an instance of InterSystems IRIS loaded with sample stock data and a Python IDE ready to go.
    tip
    Need InterSystems IRIS?
    Get a free, online development sandbox here. Log in with your InterSystems universal account, or register for one below.
  2. Clone the quickstarts-python repo into the /home/project directory in the cloud IDE whose settings are above. Open the IDE -- cannot display value - please provision a sandbox. Then in the IDE’s terminal window:
    cd /home/project
    git clone -b try-iris http://github.com/intersystems/quickstarts-python
    
  3. Using the Explorer pane (the file browser encompassing the left side of the IDE), open the quickstarts-python folder, and then the Solutions folder. Then open the connection.config file and change the value of IP to -- cannot display value - please provision a sandbox and the value of port to -- cannot display value - please provision a sandbox.
  4. Install InterSystems IRIS Native API for Python:
    cd /home/project/quickstarts-python/Solutions
    pip install nativeAPI_wheel/irisnative-1.0.0-cp34-abi3-linux_x86_64.whl
    

    If installed successfully, you should see the following message: Successfully installed irisnative-1.0.0

  5. Install InterSystems IRIS PyODBC driver:
    cd /home/project/quickstarts-python/Solutions
    sudo odbcinst -i -d -f pyodbc_wheel/odbcinst.ini
    

    If installed successfully, you should see the following message: odbcinst: Driver installed. Usage count increased to 1.

Use the Native API to store and exercise a custom structure

Use the Native API to store to a custom structure and call methods or routines.

  1. Run nativeAPIConnectToIRIS.py to see how to connect to InterSystems IRIS using the Native API.
    python nativeAPIConnectToIRIS.py
  2. After connecting to InterSystems IRIS, you can explore more of the advantages of the Native API with nativeAPIFullApp.py.
    python nativeAPIFullApp.py
    • Run option 2: Store the stock data from a .csv file in the custom structure.
    • Run option 3: Retrieve and view the stock data from this custom structure.
    • Run option 4: Call population methods within InterSystems IRIS to generate sample trades.
    • Run option 5: Print the version of IRIS to the screen. This is retrieved by calling a routine within InterSystems IRIS.
    • Run option 6: Quit.
With the Native API, you can efficiently store data in your own custom data structure. Additionally, you can call class methods and routines from within InterSystems IRIS using the Native API.

PyODBC for SQL-based access

Use the standard PyODBC for SQL-based access to relational tables.

  1. Run pyodbcConnectToIRIS.py to see how to connect to InterSystems IRIS using PyODBC.
    python pyodbcConnectToIRIS.py
  2. After connecting to InterSystems IRIS, you can now explore more of the advantages of PyODBC. This code simulates a stock trading application, allowing you to manage your personal portfolio and see how you would have done selling on a particular date.
    python pyodbcFullApp.py
    • Run option 1: View the top 10 stocks on 2016-08-12.
    • Run option 2: Create the Portfolio table.
    • Run option 3: Add three new portfolio items (for AMZN, GOOGL, and EQIX). Enter 2016-08-12 as the date, and choose a price between 200 and 300 for each item.
    • Run option 6: See how much you would have made on your portfolio if you sold on 2017-08-10.
    • Run option 7: Quit.
This code uses standard PyODBC to create, read, update, and delete data within InterSystems IRIS. With the InterSystems IRIS driver, you can easily connect and use PyODBC in the way you expect.