How to run an external script?

Hi All,

New to Ignition and Python, and my first post here.

I have a Python script that executes fine: it connects to an API, downloads the latest data and saves it as a .py file. The data is then displayed as a table in Ignition.

However, I can't execute the script it via the Script Editor.

My Python is installed here: C:/Python/python.exe
And my script is here: C:/LoadFlex/GetForecast.py

On the Script Editor I tried this:

system.util.execute(['C:/Python/python.exe', 'C:/LoadFlex/GetForecast.py'])
It didn't work. Then I tried this:

import subprocess
pythonPath = "C:/Python/python.exe"
scriptPath = "C:/LoadFlex/GetForecast.py"
result = subprocess.check_output([pythonPath, scriptPath])

With both these approaches, I don't get any errors. It looks like the script has executed successfully. But the file that is to be overwritten with new data remains unchanged So, I know it has failed.

Can anybody throw some light on this? Cheers.

1 Like

You'll want to use ProcessBuilder to get the results. Example - Procedure for installing python libraries from source - #16 by PGriffith

My guess though is it has to do with how you are formatting your file paths. I would try system.util.execute([r'C:/Python/python.exe', r'C:/LoadFlex/GetForecast.py']) The / is an escape character so you either need to do \\ to make a single / or you can put the 'r' in front to indicate it is a "raw" string and then \ no longer functions as an escape character. Did not have my coffee yet. I think the forward slashes should be ok. Use process builder and see what you get.

Never heard of Process Builder; will have a look. Thanks Mate.

Have your coffee :smiley:

1 Like

Here's what I have done:

I have put together a batch file that fires up the scripts. I can execute this batch file from Ignition Script Console! I use:
system.util.execute(["C:\LoadFlex\GetFOrecast.bat"])

But I can't run it from a Vision Client button. On my Gateway, I have a Gateway Event Message Handler and the button sends a message to the handler.

Its not happening. Any idea? Thanks everyone.

When you run it on designer, you are running it on the computer that designer is running on.

Assuming that you're Ignition gateway is installed on another different computer, when it gets the command and tries to system.util.execute(["C:\LoadFlex\GetFOrecast.bat"]) -does this file exist on your Ignition gateway server?

4 Likes

Hi PSI,

Apologies for the late reply; was out on site for a few days.

At the moment, I am trying it out on my Laptop. So, yeah the Gateway and Designer both running on my Laptop. Cheers.

So the gateway is at localhost on your laptop? If so, then I suspect an errors permission of some sort.

Run it with the ProcessBuilder script and log the output so you can see what error you are getting. This is the much easier than trying to guess what is going wrong.

Yup, Gateway's on my laptop. I will try out the ProcessBuilder. Cheers.

1 Like

Hi All,

Final Update:

Python Script in the Ignition PC C:\ (this connects to an external API and saves the returned JSON file to C:\ again.

Added a Project Script that System.Util.Execut() the above Python script, reads the saved file, converts it to a database while removing some unwanted columns and writes the resultant dataset to an Ignition Tag.

A button on the page will then call the Project Script to execute. So far works fine.

Now need to add scheduled trigger.

However, the received date time format is ISO8601, like, "2024-03-20T23:00:00Z"., which is not supported. Will start a new topic.

Cheers.

Any reason you're not scripting this in ignition directly ?

2 Likes

As Pascal said you can do HTTP requests methods with system.net.httpClient | Ignition User Manual right inside of Ignition.

Ignition has a a lot libraries that do the basic things so you can build out from there. Also, remember this is all jython so you have access to java libraries as well for times when there may not be a direct system.* call. Using built in libraries and java are always preferable to shelling out as long as you can do what you need.

IMO the only real reason to shell out is if you need to do things that you can't build with system.* or java easily which to me means things like using library like tensorflow or similar ML/AI libraries that are only on CPython of which there is no current java equivalent and building would be a project itself.

1 Like

Hi pascal and psi, thank you.

The reason was simply Deadline! Had to be able to show some numbers before the deadline.

Now that the deadline has passed, I have got system.net.httpClient in action and I am so glad its within Ignition.

This is my second month with Igntion and still exploring. Cheers.

1 Like