Hi
I have Ignition Version 8.1.3 running on Raspberry Pi under latest Linux
I have developed a Python script that runs on the Ignition Gateway machine. It has been tested and runs correctly from both the IDE and command line.
I now want run it from Ignition, eg a button press or tag event.
I have tried both system.util.execute and subprocess methods, in both Gateway Message events and tag change scripts, and cannot coax either of them to work.
Using subprocess.Popen([ sys.executable, “home/pi/myDirectory/doIt.py” ])
This throws a “No such file or directory” error
Using system.util.execute([ ‘python /home/pi/Nest/.py’ ])
This does not throw an error, but the Python script does not execute
Can someone help with:
a. Which method is preferable?
b. For the recommended method, what is the process command format?
Using system.util.execute([ ‘python /home/pi/Nest/.py’ ]) - I don’t know if you just had a transcription error typing up the question. but shouldn’t this be the same diretory as before and pointing to dolt.py? So system.util.execute([ ‘python “home/pi/myDirectory/doIt.py”]) - you’re saying this sort of statement does not throw an error and it does not run the dolt.py?
Somewhat related - as I recall python3 is not automatically aliased to python on the raspberry pi. Make sure when you type python into terminal, you see the version that your file is written in. Otherwise you might mean to be using python3.
What is the contents of dolt.py? What are you expecting to happen? I wouldn’t think you would get any errors to your log. What you’re doing is telling your raspberry pi to run dolt.py, in a separate process, outside of Ignition. So whether it works or not, Ignitiion I don’t believe would have any access to that information, unless you did it with subprocess.Popen, and had shell=True iirc - which would only feed you back the strings of the terminal as it executes, but again, you won’t be getting any errors here (except for an error in trying to run it at all, but once it goes, its it’s own process).
Now I will wait for @pturmel to correct me where I am wrong
bkarabinchak.psi
Thank you for the feedback
The python script does some image processing and writes results to a database. It runs asynchronously and no results are required to be returned to Ignition.
It also displays some images and prints some debug info to the console.
I thought this may have been an issue so I have now added a “headless” mode with no print output or image display. This mode works fine from command line. I can run this headless script and it executes correctly and writes results to database (which can be seen in Ignition). However it still does not work when I run it from Ignition/subprocess()
So I remain confused.
I have a couple of questions:
Where do I find any error messages thrown by my py script? I have checked logs in “/var/log” directory with no joy files
The only other thing I can think of is that the sql writes are failing due to user permissions? What user does Ignition service run as? I connect from the python script using “pymysql.connect(host='localhost,user=“root”,password=“rootpassword”,database=”"mydb’)"
For logging, that will go to stderr (standard error output to the console) if you haven’t routed it to a logging file. execute() won’t show that, and subprocess can, but you have to get the subprocess code right to get any stderr messages back from subprocess into variables in Python that you can print.
If you list your processes (“ps” command with appropriate arguments) you’ll be able to see what user Ignition is running as.
However, before going any further you might try to provide the full path to the python executable. The OS doesn’t always set up environment variables for execute() that include python3 in the path.
If you’re using subprocess, to get standard printed results (stdout, not stderr) one option is to do something more like:
import subprocess
result = subprocess.check_output(“/path/to/python3”, “/path/to/python/program.py”)
Also, you could instead wrap your Python program in a local web service using flask or bottle. Some folks go that direction for portability. (I’ve done it myself for home projects with image capture from a Pi camera.) More info can be found here: Python, Jython, CPython, Libraries, Pip, and Python 2.7 vs 3 - A quick primer
What user is the Ignition process running as? Typically it is “ignition”, and that won’t be able to make file changes without write permissions.
Are you sure your handler is executing? Include logging in your handler before and after system.util.execute and check the gateway log for the output. (Use system.util.getLogger() instead of using print statements.)
while when I tried 'system.util.execute(["rm","rf","/home/file/os_.py"])', I actually deleted that py file from the folder, so I think the handler is executing.
For those trying to help you, it leads to more work for them since Person A may see this topic and Person B sees the other topic. They then add answers independently of each other, to the separate posts, not realizing that the other person may have already posted the exact same answer in the other topic. They may also miss the other persons posts and have no chance to build upon them with additional thoughts.
Long story short, wastes time (making people less likely to offer assistance in the future) and can reduce the quality of the answers you will get in the end. If you see a relevant topic that you would like to pull into the discussion, you can post in your original topic with a link to the relevant second topic.
I did a project for ML using python3 and I use flask to integrate with my py script , the py files are in my personal PC and I call it from ignition perpective window by :