# Running external python script in Ignition

**URL:** <https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [March 8, 2023, 9:50am UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769 "2023-03-08T09:50:19Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lukasz\_Wieckowski](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lukasz\_Wieckowski](https://forum.inductiveautomation.com/u/Lukasz_Wieckowski)\
**Post date:** [March 8, 2023, 9:50am UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/1 "2023-03-08T09:50:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [March 8, 2023, 9:58am UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/2 "2023-03-08T09:58:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [March 8, 2023, 10:06am UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/3 "2023-03-08T10:06:18Z")

</div>

```auto
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](https://docs.inductiveautomation.com/display/DOC81/Scripting+in+Ignition)

---

<div class="post-metadata">

**Author:** ![Lukasz\_Wieckowski](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lukasz\_Wieckowski](https://forum.inductiveautomation.com/u/Lukasz_Wieckowski)\
**Post date:** [March 8, 2023, 10:57am UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/5 "2023-03-08T10:57:05Z")

</div>

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":

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/1/016bf9e6eb0f363bca473a9d76fc60b8f6ba1284.png)

Unfortunately the external script is still not executing

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [March 8, 2023, 11:46am UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/6 "2023-03-08T11:46:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 8, 2023, 12:14pm UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/7 "2023-03-08T12:14:14Z")

</div>

> [@Matrix\_Engineering](#):
>
> For Vision, call a project script from your button, then the code will execute on the Gateway.

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.

> [@Matrix\_Engineering](#):
>
> In Perspective, I believe the best way is to send a message handler.

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

> [@Lukasz\_Wieckowski](#):
>
> I use perspective.

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.

---

<div class="post-metadata">

**Author:** ![Lukasz\_Wieckowski](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lukasz\_Wieckowski](https://forum.inductiveautomation.com/u/Lukasz_Wieckowski)\
**Post date:** [March 8, 2023, 2:12pm UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/8 "2023-03-08T14:12:21Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/0/9085e186166e021232176a8fdb20359ba3fadeba.png)

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/5/a52b4d27e6deab5a697cbbbede9658d474c175ae.png)

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/7/871f913d6e837991f3871c4bcabc4f6068ad8f89.png)

But external script don't execute

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 8, 2023, 2:12pm UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/9 "2023-03-08T14:12:52Z")

</div>

What user is your Ignition service running as?

---

<div class="post-metadata">

**Author:** ![Lukasz\_Wieckowski](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lukasz\_Wieckowski](https://forum.inductiveautomation.com/u/Lukasz_Wieckowski)\
**Post date:** [March 8, 2023, 2:13pm UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/10 "2023-03-08T14:13:36Z")

</div>

Where can i check it?

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 8, 2023, 2:14pm UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/11 "2023-03-08T14:14:17Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/d/ad2c9942b4b8b29b0172e73f312ac3da3423167c.png)

---

<div class="post-metadata">

**Author:** ![Lukasz\_Wieckowski](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lukasz\_Wieckowski](https://forum.inductiveautomation.com/u/Lukasz_Wieckowski)\
**Post date:** [March 8, 2023, 2:15pm UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/12 "2023-03-08T14:15:49Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/b/abc94a9e22c41cf767a742d07ad46c17636104f6.png)

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 8, 2023, 2:30pm UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/13 "2023-03-08T14:30:13Z")

</div>

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](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/5/f5276161e57e3a64e8e28dad24210081f676e75e.png)

---

<div class="post-metadata">

**Author:** ![Lukasz\_Wieckowski](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lukasz\_Wieckowski](https://forum.inductiveautomation.com/u/Lukasz_Wieckowski)\
**Post date:** [March 8, 2023, 2:59pm UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/14 "2023-03-08T14:59:27Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/1/d1f348d81291a670e6f845906ea44b33c6c39451.png)

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 8, 2023, 3:03pm UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/15 "2023-03-08T15:03:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Lukasz\_Wieckowski](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lukasz\_Wieckowski](https://forum.inductiveautomation.com/u/Lukasz_Wieckowski)\
**Post date:** [March 8, 2023, 3:30pm UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/16 "2023-03-08T15:30:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Lukasz\_Wieckowski](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lukasz\_Wieckowski](https://forum.inductiveautomation.com/u/Lukasz_Wieckowski)\
**Post date:** [March 9, 2023, 9:45am UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/17 "2023-03-09T09:45:47Z")

</div>

I have this configuration now:

- Ignition Service log as: "Usługa lokalna" (ang. local service)  

- "Usługa lokalna" have full access to the folder with python script C:\Praca\Testy kamer\test.py:  

- "Usługa lokalna": have full access to the folder with python311 C:\Program Files\Python311\python.exe  

- "Usługa lokalna" have full acces to the folder where python script save image from camera:  

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?

---

<div class="post-metadata">

**Author:** ![Lukasz\_Wieckowski](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lukasz\_Wieckowski](https://forum.inductiveautomation.com/u/Lukasz_Wieckowski)\
**Post date:** [March 9, 2023, 10:00am UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/18 "2023-03-09T10:00:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [March 14, 2023, 2:51pm UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/19 "2023-03-14T14:51:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Lukasz\_Wieckowski](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lukasz\_Wieckowski](https://forum.inductiveautomation.com/u/Lukasz_Wieckowski)\
**Post date:** [March 14, 2023, 3:13pm UTC](https://forum.inductiveautomation.com/t/running-external-python-script-in-ignition/70769/20 "2023-03-14T15:13:08Z")

</div>

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.
