Leveraging the Power of Python in LabVIEW

Tags: LabVIEW

This post is going to focus on a few of the tools currently available for integrating Python and LabVIEW together in your application.  In a follow-up post we will be discussing a tool that WTI has developed internally for use in an application-scripting framework - so stay tuned.

Edit:  We've now released TestScript, the free bidirectional Python/LabVIEW Connector


Why Python?

The first thing I want to talk about is a couple of reasons why Python is becoming an increasingly popular tool for software development.

  • Easy to Learn - The syntactic simplicity of Python allows for newcomers to get up-to-speed in a short amount of time.

  • Cross-platform Compatibility - Python will not only run on Windows, but it will also run great on Linux and Mac machines as well.

  • Strong community backing - There is a large community supporting Python, and because of this there is a large array of Python libraries available for use that can provide a solution to virtually any problem.

Inside-out vs. Outside-in

When it comes down to exactly how you integrate Python into your LabVIEW application, the tools available now typically fit into one of two paradigms:  Inside-out or Outside-in.  The diagram below depicts the two different styles, but to put it simply, Inside-out is defined as LabVIEW instructing Python to perform an action and return a result, while Outside-in is defined as Python instructing LabVIEW to perform an action and return a result.

LabVIEW python inside out outside in.png

INSIDE-OUT (Using LabVIEW to call Python)

The simplest example here is using the System Exec VI to launch Python and pass it information through the standard input pipe or through arguments to the Python script.  Then, Python can return the result on the standard output pipe, and generate errors on the standard error pipe.

System Exec

LabVIEW python system exec.png


Enthought Python Integration Toolkit

Enthought provides a nice API for integration Python into your LabVIEW application.  They offer both a pro and free model, but there are a lot of nice features in the free model.  In the example below, a file named mymodule.py is used and the square_it function of that module is called, and the result returned.  Enthought does a great job and simplifying this workflow in a concise manner.

Enthought Python.png

OUTSIDE-IN (Using Python to Control LabVIEW)

You can build any LabVIEW application into a DLL (Dynamic Linked Library) and then access this DLL from Python.  A DLL built in LabVIEW will expose the methods you have configured to Python.  The example below demonstrates how, in Python, you can call a DLL built in LabVIEW.

 Calling a LabVIEW DLL from PythonOutside In implmenetation example.png

Another way to implement the outside-in architecture is by using a RESTful Web API Service in LabVIEW.  REST stands for Representational State Transfer - in most cases, it is an HTTP API for your application.  You can call methods into LabVIEW by sending HTTP requests to the server.  Python can execute these HTTP requests with relative ease as is seen in the example below.

Calling LabVIEW through a RESTful Web API Service

Calling Labview restful.png


Overall, there are a good selection of options out there for integrating Python into your LabVIEW application (or, for that matter, integrating LabVIEW into your Python application).  Each methodology has its pros and cons, so the specific architecture you choose will depend on your needs.  This blog post isn’t a comprehensive list of all the options either, so be sure to take a look online, and at the additional resources below, to see if there is something else out there that fits your needs.

Additional Resources