Traditionally, accessing the PI System from the Python programming language can be challenging. See for example these threads.
The primary Python language implementation, CPython, is based on C. The PI SDK, an older PI Developer Technology, is based on COM, while AF SDK, a widely-used PI Developer Technology, is based on .NET. These differences in platform require interfaces or other wrappers that must be used as a bridge, but they can be cumbersome to use.
For example, to use PI SDK within CPython, the pywin32 library can be used to allow Python to communicate with COM classes. For AF SDK, the adodbapi library can be used to access AF via PI OLEDB Enterprise. Additionally, Python for .NET may be an option but this has not been well explored. Regardless, PI SDK will be deprecated and PI OLEDB may not be suitable in some cases. PI ODBC and pyodbc looks interesting though and may be worth exploring more...they almost sound like they were made for each other...
An alternative approach is to use a .NET implementation of the Python language, such as IronPython. However, one of the strengths of Python is its wide variety of analytics libraries, such as numpy, scipy, pandas, and scikit-learn. These analytics libraries call into highly optimized C and Fortran numerical routines, but integration of these Python libraries into the .NET runtime via IronPython has been historically challenging.
PI Web API
The addition of PI Web API to the suite of PI Developer Technologies vastly increased the accessible surface area of PI from various programming platforms. One of the benefits of exposing a RESTful API is that much of the web has adopted this architectural style, and hence, the API communicates with programmers via a common "language" (i.e. HTTP). Programmers really need to just understand the basics of formulating an HTTP request and handing a response, and do not need to download custom SDK's or deeply interact with and understand that SDK's object model. Once the basics of HTTP are understood, then the rest is just implementation in the desired programming language (Of course, it is not "just" so simple. I'm looking at you, OAuth... )
In addition, RESTful API documentation all look fairly similar, so once PI Web API is understood, for example, it is easier to pick up API's from Facebook, Google, Esri, etc., enabling apps that can connect to multiple services and provide greater contextual experience for the user.
This blog post will present YAWAPP (Yet Another Way to Access PI from Python). But hopefully, it will motivate some compelling use cases. As mentioned, RESTful API's can provide a standardized approach for data integration. Bringing PI System data into Python, particularly CPython, allows this data to be consumed by Python's extensive, optimized numerical libraries. Using PI Web API in Python can also be used to expose PI data in web and mobile applications via Python's popular web application framework, Django.
Disclaimer: I am not a Python developer. I've used Python once 3 years ago and then only recently. But hopefully, I'll demonstrate how easy it is to get started when working with RESTful API's, even with little prior experience.
The complete source code examples for this post were hosted on a personal Github repo that is not support by OSIsoft. The official OSIsoft github repo contains samples for using PI Web API with python.
There are many distributions of Python language implementations. For example, here is a small list:
Here, I will be using the Python distribution from python.org (CPython), namely version 2.7.10 for Windows.
There are many IDE's to choose from. As mentioned, I'm not a Python developer and previously, I hacked away with Notepad++... I decided to use a more extensive IDE this time and chose PyCharm (Community Edition). Why? It was the first one that came up in the google search , so you are free to explore your own.
I found PyCharm offers many features familiar to Visual Studio users (intellisense, code completion, static analysis, debugging, project file/package management, etc.), but the Community edition did not have the "streamlined" feel of Visual Studio, which can be expected. I will say it does seem to be more enjoyable to program in Python than C#...
Which Python packages?
I imported three packages into my example project.
requests is used as the HTTP client library and seems to be the most popular within the Python community. Amazon, Google, and even POTUS use it. json as the name suggests helps me deserialize JSON text into Python dictionaries and vice versa. bunch is a package that provides a wrapper class around Python dictionaries so I can browse the dictionary using dot notation (e.g. dict.key instead of dict["key"]), evocative of the C# anonymous type.
Overview of Examples
I assume the reader is somewhat familiar with the PI Web API already and its basic operations. Below are some introductory resources to get started:
I show how to browse AF databases, elements, and attributes using PI Web API along with the requests library. I also show how to retrieve the current value of an attribute, write to that attribute, and modify its properties. What is really shown, however, are how to perform the following using requests:
- How to use different HTTP verbs (GET, POST, PATCH in my examples) using requests
- How to pass in query string parameters in the URL
- How to set request headers (i.e. Content-Type: application/json)
- How to send JSON request payloads (body)
- How to parse the incoming JSON using json and bunch
All of these are well-documented by the requests authors. It could be said that, in my examples, I just substituted their URL strings for ones relevant to PI Web API
Something that is not at all trivial though is authentication via requests. Out of the box, it supports Basic authentication but not Kerberos. If PI Web API can only be authenticated via Kerberos, then the requests-kerberos package must also be installed.
Examples on GitHub
The OSIsoft github repository contains sample code for using the PI Web API with Python.