Python QuickStart

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.

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 and a Python IDE ready to go.

    If you don’t have InterSystems IRIS yet, 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 . 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 (provision a sandbox to get this setting) and the value of port to (provision a sandbox to get this setting).
  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 python nativeplaystocksTask1.py to see how to connect to InterSystems IRIS using the Native API.
  2. After connecting to InterSystems IRIS, you can explore more of the advantages of the Native API. Run python nativeplaystocksTask6.py.
    This code stores stock data in a custom data structure.

    • Run option 2: Store the stock data in a 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 better information for 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 python pyodbcplaystocksTask1.py to see how to connect to InterSystems IRIS using PyODBC.
  2. After connecting to InterSystems IRIS, you can now explore more of the advantages of PyODBC. Run python pyodbcplaystocksTask7.py.
    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.

    • 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) using a value between the opening and closing price on 2016-08-12.
    • 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.