Skip to main content
Version: 8.1

Python Scripting

About Python​

While it is entirely possible to create a complete and powerful project in Ignition without writing a line of script, many designers will find that in order to complete projects with specific requirements, they need to learn at least a little Python. In our experience, most industrial projects involve lots of very complex and specific requirements.

This section is a short tutorial specifically for python, which should help get you started. It goes over all of the core concepts you will need for scripting in Ignition but the next section (Scripting in Ignition) goes over using Python directly inside Ignition.

Python or Jython?​

You'll often hear Python referred to as "Jython" by advanced users of Ignition. Python is the language, Jython is the implementation of the language that we use. Most users of Python use the implementation called "CPython" - they just don't realize it. See http://en.wikipedia.org/wiki/Python_(programming_language)#Implementations.

One of the powerful things about using Jython is that your script has access to the entire Java standard library. For more information, see Accessing Java.

Many scripting users are blown away by their script's speed. We can't take credit for this - the Jython engine compiles the code when it is run. your Jython code is converted to Java bytecode, which means it runs natively in the JVM, which in turn can compile it to machine code. It's fast.

Which Version of Python is Used?​

Ignition uses Jython 2.7. Jython is the Python programming language implemented over the Java Virtual Machine. When looking at outside documentation, such as on www.python.org, verify that you are looking at the correct version.

Jython 2.7 allows us to use the standard functions and tools in Python 2.7, so if you want to look up something in the Python docs, make sure to use version 2.7 (https://docs.python.org/2/).

Scripting Basics​

Inductive University

Basic Python - Variables and Comments

Watch the video

Python is very easy to learn, and with some understanding of its basic syntax, you can get started making your own scripts.

Hello World​

Let's get right to everyone's favorite example, "Hello World." The following script will print out "Hello World" to the output console.

The print keyword is a handy tool in Python, allowing you to write text to the Output Console. This is useful for debugging your scripts. You can print multiple things by separating them with commas.

Variables​

Variables are created by simply assigning a value to them. Variables do not need to be declared, because Python has a dynamic type system. That means Python figures out the type of the variable on the fly when the script is executed.

The following script would print out: 15

Python - Using Variables
x=5
y=3
print x*y

Strings​

Strings are defined in Python with a matching pair of 1 or 3 single or double quotes. There are few times when the type of quotation mark you use matters - but one common reason to choose one or the other is for 'escaping' other quotes inside your content. Some of the rules are shown here:

Python - Using Single and Double Quotes
print "This is my text"     # Using double quotation marks
print 'This is my text' # Using single quotation marks
print "This is my text' # This will not work because python does not allow mixing the single and double quotation marks
print "My name is 'David'" # This will print: My name is 'David'
print 'My name is "David"' # This will print: My name is "David"
print 'My name is Seamus O\'Malley' # This will print: My name is Seamus O'Malley

Triple quotes (single or double) can be used to make 'escaping' both single and double quotes inside your string easier, or to write multi-line comments:

Python - Multiple Lines of Comments Using a Triple Quote
'''
This is a lot of text
that you want to show as multiple lines of
comments.
Script written by Professor X.
Jan 5, 1990
'''
print 'Hello world'

Strings can also be prefixed with certain characters to change how they are interpreted - for instance, a leading u character marks a string as Unicode, allowing for characters outside of the ASCII range to be used.

Python - Unicode Prefix on String
print u"Àâü"

Whitespace​

Perhaps Python's most unique feature is logical blocks which are defined by an indentation in Python. A colon (:) starts a new block, and the next line must be indented (typically using a tab or 4 spaces). The block ends when the indentation level returns to the previous level. For example, the following will print out "5 4 3 2 1 Blast-off" with each value on a new line. The final print is not part of the while loop because it isn't indented.

Python - Logical Blocks / Indentation
countdown = 5
while countdown > 0:
print countdown
countdown = countdown - 1
print "Blast-off!"

Comments​

Comments are a way to document your Python script. There are several ways to use comments, but the best advice we can give is to use them as much as possible! There are a few ways to make a comment in Python.

Individual Lines​

You can start a line with a pound/hash (#) sign, or put one anywhere in a normal line of code.

Python - Document Scripts Using Comments
# this is a comment
print 'Hello world' # this is also a valid comment

Blocks of Lines​

While Python doesn't explicitly have a way to block comment (comment out multiple lines), multi-line strings are functionally similar, and a common convention.

Comment Many Lines with a Keyboard Shortcut​

In Ignition, you can use the Ctrl-/ keyboard shortcut to comment several lines of code at once. Just highlight one or more lines of code and hold the Ctrl key and press "/". This will prepend all of the selected lines of code with the pound/hash (#) sign. Press Ctrl-/ again to remove the pound/hash sign.

Control Flow​

Inductive University

Basic Python - Flow Control

Watch the video

Control Flow statements, that is the ifs and loops, make the language do things differently based on the various conditions. Python has all of the basic control flow statements that you'd expect from a programming language.

If and Else​

An if statement allows you to check if a condition is true or not true. Depending on the condition, you can either execute a block of code, or do something else. Many of these can be chained together to determine under what conditions should certain code execute.

Pseudocode - If Statement
if condition == True:
print value1

For and While​

Looping can be done with a for, which executes a block of code a set number of times, or a while, which executes a block of code until a certain condition is met. Both can be incredibly useful.

Pseudocode - For Statement
for item in myList:
print item