Python scripting problem

#1

I’m using Ignition 7.9.8 and would like to use the python ‘importlib’ module and importing it in a button pressed event scripting area causes an error saying 'no module named import lib.
I need this to import and call functions out of a script external to the ignition system.
Basically, the following line:
import importlib.util
does not work.

Any help?

0 Likes

#2

Note that v7.9 jython uses python 2.5. Python libraries that belong to version 2.7 and up will not work. They also must not have any dependencies on other languages like C.

0 Likes

#3

These are modules that I have written myself. I just want to call them from within Ignition. The link below has the directions I followed:

https://support.inductiveautomation.com/index.php?/Knowledgebase/Article/View/98/2/importing-and-using-3rd-party-python-libraries-in-ignition

After seeing the import fail (“Error: ‘No module named [module_name]’”)

Any other things I am missing?

0 Likes

#4

So you imported your .py files to C:\Program Files\Inductive Automation\Ignition\user-lib\pylib and reloaded your script console?

Maybe try
from importlib import util?

0 Likes

#5

How do I reset the script console?

0 Likes

#6

bfuson,

Yes, I placed my py file (1) in the pylib directory and also tried placing that file in yet another directory and adding the from import myclass

this yielded the same results. no module named myclass

0 Likes

#7

Per https://docs.inductiveautomation.com/display/DOC80/Script+Console
Click the reset icon

0 Likes

#8

OK. Still the same thing after the ‘reload’

0 Likes

#9

Share your module?

0 Likes

#10

Pretty simple, just trying to show a dialog that proves the script was run/called.

Here is the source:
from tkinter import messagebox

class iaUtils():
def DisplayMessage(_message):
messagebox.showinfo(“Information”,“iaUtils recieved:” + _message)

0 Likes

#11

tkinter is the standard python interface for tcl-tk. Tcl is a C library, which means it’s effectively off-limits for usage in Ignition/Jython. There is a Java implementation of tcl, but you probably don’t want to go down that road.

In Ignition you’re much better off sticking with java.awt or javax.swing for display purposes. The easiest way to say, open a message box, is by using Ignition’s built-in system.gui.messageBox() function.

0 Likes

#12

MessageBox was only trying to show the script was called. Didn’t realize how limited the extensibility of the scripting is. I will ttry to reform my script and see how it goes.

0 Likes

#13

You are writing about two different modules here. importlib.util is a Python 3 system module that is not available in Ignitions Phyton 2.5.
If your module references importlib you need to remove that reference.

0 Likes

#14

@chi is also correct about importlib

The knowledge base article you linked is clear about both cases:

  1. The library or module is not written in C and is not dependent on C / C-Python features
  2. The library is Python 2.5 compatible (*as of Ignition 7.8, Ignition uses Jython 2.5)

Ignition’s scripting facilities are top-tier as far as convenience and extensibility are concerned. The default GUI toolkit you get with Jython just happens to be swing, not tcl.tk. I believe you could, in theory, package up Jacl in an Ignition module, import it, and create tk GUIs in Jython.

Or you can code up screens using swing in Jython.
Or you can use the Vision Designer to graphically create windows and then call them through scripting.

https://jython.readthedocs.io/en/latest/chapter16/

0 Likes

#15

Can there by any imports? For example ‘import os’?

0 Likes

#16

Yes, most python standard library imports work fine:

os, sys, re, functools, subprocess, __future__, etc...

You can even import objects from Java:

from java.lang import reflect
from javax.swing import SwingUtilities, JComponent
from java.io import File, FileWriter

And from the Ignition namespace:

from com.inductiveautomation.factorypmi.application import FPMIApp, VisionDesktop

Just to give a few examples.

1 Like

#17

Nice. Just needed the clarification. At first it seemed like you were saying I could not yse ANY imports in he script I’m calling from Ignition side.

0 Likes

#18

I have asked this elsewhere, but am going to ask here also, since you guys seem to be responding and knowledgeable.

IN working with an Easy Chart component, I would like to interrogate the Chart in python and retrieve a list of tags (with their paths) that the chart is using. I have some extra data I would like to value-add to the chart.

Any thoughts?

0 Likes

#19

Tag pens are contained in a dataset in the chart properties. The scripting reference would be <chartComponent>.tagPens.

0 Likes

#20

Yes, i’m confused though. My Dataset Viewer is full to the gills with rows, but my event.source.parent.getComponent(“Easy Chart”).tagPens.RowCount is 0.

0 Likes