Running external python script in Ignition

Hello everyone. I write external python script, which connect to IP camera, take a screenshot and saves the photo to a folder on the disk. I use system.util.execute to run this script from Ignition:
system.util.execute(['C:/Program Files/Python311/python.exe', 'C:/Praca/Testy kamer/test.py'])

When I run this line of code from the script console in ignition - the script executes correctly and the image is written to disk.

But if I run system.util.execute function from button event on click - external script does not execute.

How to run external script from button event in Ignition?

Welcome!

You need to understand the different scopes.

You didn't mention if this is Vision or Perspective.

For Vision, call a project script from your button, then the code will execute on the Gateway.

In Perspective, I believe the best way is to send a message handler.

Script Scope
One important thing to keep in mind before scripting in Ignition, is to understand the concept of scope. 

Within Ignition, there are different scopes:

Gateway Scope - The script runs on the gateway. Scripts running in this scope cannot interact with components in the other two scopes. 
Perspective Session Scope - The script runs as a part of a Perspective Session. Note that scripts in Perspective execute on the gateway, not in the browser, but this scope is still distinct from the Gateway Scope. 
the Vision Client Scope - The script runs inside of an instance of a Vision Client. 
Where a script was written determines which scope it executes in. For example, Tags are in the Gateway Scope, so Tag Event Scripts execute in the Gateway Scope.

This means that the script will not be able to access any client level resources such as windows or components that you may have open in the Client. Additionally, some of the system functions like system.gui.errorBox only work in the "Client Scope," so you will not be able to use them in the script on the Tag.

https://docs.inductiveautomation.com/display/DOC81/Scripting+in+Ignition

4 Likes

I use perspective. Now I test message handler. In button event I write code:
system.util.sendMessage(project='Camery',messageHandler='ReadCamera', scope = 'G')

In gateway event scripts -> gateway message handlers i make handler = "ReadCamera":

Unfortunately the external script is still not executing

I am not a message handler wizz but I will comment that "C:/" refers to the C hard drive on your gateway, not your local PC, if you are running a remote designer session.

1 Like

This is not correct. Project scripts run in the same scope where they are called. They don't have a defined scope of their own. If a Vision button needs something to run in gateway scope, it needs to use a message with a handler in gateway scope.

All Perspective scripts run in the gateway. No need for a message handler.

You can only call python scripts that exist in the gateway, not in the environment of the browser running the Perspective session. Browsers don't permit local code execution (hackers would love that).

And the script in the gateway must be in a location where the gateway background service has folder and/or file permissions.

5 Likes

Python script exist in the gateway. I set permission for folder with python for "service" (USŁUGA) - full control:

For folder with script for "service" - full control:

And for folder where I want save image from camera - full control:

But external script don't execute

What user is your Ignition service running as?

Where can i check it?

If you search for Services in windows 10 you can find this -

I don't know the language but I assume that says local user.

If you right click the service, then under logon you can have Igintion log on as that user you gave permissions to, then try agian. Think you have to stop/start the ignition service after changing logon.

image

Here i have local system account. I try check option: allow service to interact with desktop, but this didn't solve the problem

Change the second option and have it log in as the person you assigned permissions to, the USLUGA user or group whatever it may be. I think it better to have Ignition log on as a specific user for permissions issues anyways. Makes things much easier/more straight forward than using a local user account imo.

I change the second option and log as usluga lokalna, but when i try to start the ignition service it turns on for a while then shuts down after a few seconds and the gateway is stopped

I have this configuration now:

In perspective appliacation I have button with on click event and script:
system.util.execute(['C:/Program Files/Python311/python.exe', 'C:/Praca/Testy kamer/test.py'])

Unfortunately this configuration doesn't run an external python script.
When I run:
system.util.execute(['C:/Program Files/Python311/python.exe', 'C:/Praca/Testy kamer/test.py'])
from the script console in ignition - the external script executes correctly and the image is written to disk.
How to run external python script from button event in Ignition?

I try another jython script to run external python script:

import subprocess
pythonPath = "C:/Program Files/Python311/python.exe"
scriptPath = "C:/Praca/Testy kamer/test.py"

result = subprocess.check_output([pythonPath, scriptPath])

Similarly to the above, launched from the script console works correctly, but from the button event it does not.

Is that script in that location on your gateway computer? It is running from your gateway.

Yes that script is in that location on my gateway computer and when i call it from cmd is running. I try call another simple script which only create text file and save it on the disk and this script run good from button event. I think, that mayby my script to connect with camera and save image to folder is taking too long to execute.