Python QuickStart

10 minutes
estimated time of completion.
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
Python is only available on InterSystems IRIS v2019.2+.
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. You can use your favorite IDE that supports Python, such as PyCharm. If you use your IDE, please verify your operating system is in the supported list below.
Windows Mac Linux
Windows 10, Windows Server 2012, 2016, 2019 Mac OS 10.13, 10.14 Oracle Linux 7, Red Hat Enterprise Linux 7, SUSE Linux Enterprise Server 12, Ubuntu 18.04, CentOS-7
 
Note
If you don’t have InterSystems IRIS set up yet, get a free development sandbox here.

Setup

  1. Verify you have an instance of InterSystems IRIS and an IDE that supports Python (such as PyCharm). If you are using AWS, Azure, or GCP, verify that you have followed the steps to change the password for InterSystems IRIS.
  2. Load sample stock data into InterSystems IRIS:
    1. Clone or download this repository: https://github.com/intersystems/Samples-Stock-Data. This repository has a folder named data, which contains all files needed to load the sample stock data.
    2. In the Management Portal, navigate to System Explorer > Classes and import DemoStockCls.xml and StocksUtil.xml files into the USER namespace from the data folder.
    3. In the InterSystems IRIS Terminal, type the following command. Replace repo_home with the directory of Sample-Stock-Data repo that you recently cloned: do ##class(Demo.Stock).LoadData("repo_home/data/all_stocks_1yr.csv")
  3. To get the sample Python code, download or clone the following repo into your IDE: http://github.com/intersystems/quickstarts-python
    1. Navigate to the Solutions directory.
    2. Open connection.config file and modify the IP and password to be the correct values for your InterSystems IRIS instance. Although port and username are most likely the defaults, you should verify that the values are correct.
  4. Install InterSystems IRIS Native API for Python:
    • Windows: pip install nativeAPI_wheel\irisnative-1.0.0-cp36-cp36m-win_amd64.whl
    • Mac: pip install nativeAPI_wheel/irisnative-1.0.0-cp34-abi3-macosx_10_13_x86_64.macosx_10_14_x86_64.whl
    • Linux: 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:
    1. Verify you have PyODBC installed locally. If not, please see how to install PyODBC. If you use InterSystems Labs Sandbox, PyODBC is already installed for you.
    2. Configure PyODBC for InterSystems IRIS:
      1. InterSystems Labs Sandbox: PyODBC is already installed for you.
      2. Windows: pyodbc_wheel\ODBC-2018.1.1.635.0-win_x64.exe
      3. Mac OS: odbcinst –i –d –f pyodbc_wheel/mac/odbcinst.ini
      4. All others: odbcinst –i –d –f pyodbc_wheel/linux/odbcinst.ini
    3. If installed successfully:
      1. Windows: The .exe file finishes without any error.
      2. All others: 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. Open nativeplaystocksTask1.py to view the connection string. You will notice it uses these variables to connect: const connection = irisnative.createConnection(ip, port, namespace, username, password)
  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. Open pydbcplaystocksTask1.py to view the PyODBC connection string. You will notice it uses these variables to connect: connection_string = "DRIVER={};SERVER={};PORT={};DATABASE={};UID={};PWD={}".format(driver, ip, port, namespace, username, password)
  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.