You can help by commenting or suggesting your edit directly into the transcript. We'll review any changes before posting them. All comments are completely anonymous. For any comments that need a reply, consider emailing training@inductiveautomation.com.
Version:
LESSON
Third Party Python Modules
Description
Learn how to use built-in Python modules in your scripts or use your own custom modules.
Video recorded using: Ignition 8.0
Transcript
(open in window)[00:00] Sometimes when working with Ignition scripts it can be useful to import Python libraries from outside of Ignition. Ignition allows you to use a wide variety of libraries that come standard with Python too, but it also allows you write and include your own Python libraries. Let's start by looking at how to import a Python library into an Ignition script. So in the designer here I'm going to click on tools and then click on script console. Now I'm going to use the script console for this example, but we import code here in the same way we'd import it anywhere else in Ignition. Importing libraries in Python is pretty straight forward. Let's try importing the random library. This is a built-in Python library for handling random number generation. To import it I simply need to type import random. That's actually all it takes, so now let's try calling a function in random. So let's print random.randint 0,10. This will print a number between zero and 10. So if I click execute we get the number two. I'll just click execute a few more times. We see we're getting different numbers every time. Now Python syntax also allows us to import libraries in a slightly different way. Here I imported the entirety of the random library, but if I wanted to I could instead type from random import randint. Then when I print the number instead of calling random.randint I can simply type randint. This is all out of the box Python functionality and it allows us to use many kinds of useful Python functions that aren't otherwise available. In addition to this we can import our own Python libraries. To do this I'm just going to pull up my file browser here. And I've already taken us to the directory we need to use. Inside your ignition program files directory. So let's go to ignition, we can find user lib, and then we can select pylib and any Python files, that is files with the .py extension, that we put in this folder in the Ignition installation directory will be importable within Ignition scripts. So for instance that's where my random library comes from. It's available because it's from this pylib directory. So that about wraps up importing Python libraries using built-in Python libraries as well as building our own can help streamline and simplify the Ignition development process.