Python Standard Library
Python has an extensive standard library that provides a host of new functionality to the scripting language. The python documentation goes over all of the libraries in its standard library as well as how to use them here: https://docs.python.org/2/library/index.html
Let's take a look at an example of using a common library:
Importing 3rd Party Libraries
In addition to the standard libraries, 3rd party libraries can also be imported into Ignition's scripting environment. A Python Library or individual module file will consist of a python file (.py) that contains the code that implements the functions of the library. You can often find python libraries built by other users on the web, or can even create your own. These files can then be placed into a folder within your Ignition server.
- Windows folder: C:\Program Files\Inductive Automation\Ignition\user-lib\pylib
- Linux folder: /usr/local/bin/ignition/user-lib/pylib
- Mac OS X folder: /usr/local/ignition/user-lib/pylib
Once the python file is in that folder, you can then import the library into a script just like any of the standard libraries.
Ignition uses Python version 2.7. This means that any imported libraries must be compatible with Python 2.7.
Scripting in Ignition executes in the java based implementation of Python called Jython. (See Python or Jython?). While this doesn't have any great effect on the Python language itself, one of the great side benefits is that your Python code can seamlessly interact with Java code as if it were Python code. This means that your Python code has access to the entire Java standard library.
To use Java classes, you simply import them as if they were Python modules. For example, the following code will print out all of the files in the user's home directory. This code uses the Java classes
java.lang.System and java.io
.File to look up the user's home directory and to list the files. Notice that we can even use the Python-style for loop to iterate over a Java sequence.
You can find the reference documentation for the Java standard class library (also known as, the "JavaDocs") at: http://docs.oracle.com/javase/8/docs/api/
You can also create Python classes that implement Java interfaces. You do need some understanding of Java and object-oriented programming concepts, which are outside the scope of this manual. To create a Python class that implements a Java interface, you simply use the interface as a superclass for your Python class. For example, we could augment the example above to use the overload java.io.File.list(FilenameFilter). To do this, we'll need to create a
FilenameFilter, which is an interface in Java that defines a single function:
boolean accept(File dir, String name)
To implement this interface, we create a Python class that has
.FilenameFilter as its superclass, and implements that Java-style function in a Python-esque way.