# Run remote Python script on Gateway

**URL:** <https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876>\
**Category:** General Discussion\
**Tags:** perspective\
**Created:** [December 8, 2023, 3:45am UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876 "2023-12-08T03:45:12Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrd](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jrd](https://forum.inductiveautomation.com/u/jrd)\
**Post date:** [December 8, 2023, 3:45am UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876/1 "2023-12-08T03:45:12Z")

</div>

Hi,  
I would like to run a Python script on the gateway machine from a button on Perspective client.  
I am sending a message to Gateway to fire the script. The gateway event script is:

> Blockquote  
> def handleMessage(payload):  
> # F3 message handler  
> logger = system.util.getLogger("F3\_log")

```
# Logic to execute the external Python program
param1 = payload.get("param1")
param2 = payload.get("param2")

command = ["C:\Users\Ross\AppData\Local\Programs\Python\Python311\python.exe", r"C:\Users\Ross\source\repos\PythonApplication1\PythonApplication1.py"]

# Execute the external script
result = system.util.execute(command) # Remove timeout=5000
            
    
# Process the result as needed   
logger.info("GATEWAY HANDLER _> " + str(param1) + " ...rd f3.... " + str(param2))
logger.info(result)	
logger.info(str(command))

```

> Blockquote

This execute without error and the logger.info works as expected.  
But the Python script does not run

Any ideas

---

<div class="post-metadata">

**Author:** ![Hayden\_Watson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hayden_watson/32/48792_2.png) [@Hayden\_Watson](https://forum.inductiveautomation.com/u/Hayden_Watson)\
**Post date:** [December 8, 2023, 9:29am UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876/2 "2023-12-08T09:29:23Z")

</div>

I think the recommendation for doing this kind of thing wuld be to configure a Python flask server, and then using that to run remote python scripts.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [December 8, 2023, 9:50am UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876/3 "2023-12-08T09:50:45Z")

</div>

jrd, see tips on [How to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831).

---

<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:** [December 8, 2023, 12:41pm UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876/4 "2023-12-08T12:41:55Z")

</div>

Also, you don't need to use a message. Perspective button jython scripts run on the gateway anyways.

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [December 8, 2023, 12:47pm UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876/5 "2023-12-08T12:47:34Z")

</div>

> [@jrd](#):
>
> `"C:\Users\Ross\AppData\Local\Programs\Python\Python311\python.exe"`

backslashes are escape characters, unless it already got escaped because of the bad formating xd

---

<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:** [December 8, 2023, 1:05pm UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876/6 "2023-12-08T13:05:31Z")

</div>

You're never going to get anything in your `results` variable as `system.util.execute` returns nothing, it will always come back as a `None` - [https://docs.inductiveautomation.com/display/DOC81/system.util.execute?gclid=CjwKCAiAmsurBhBvEiwA6e-WPL18j1xtrRLWguomUD-Gn9jJEjnQ5b8akXKsQH-FiV35aPRM1jAPwRoCmZYQAvD\_BwE](https://docs.inductiveautomation.com/display/DOC81/system.util.execute?gclid=CjwKCAiAmsurBhBvEiwA6e-WPL18j1xtrRLWguomUD-Gn9jJEjnQ5b8akXKsQH-FiV35aPRM1jAPwRoCmZYQAvD_BwE)

`system.util.execute` is fire and forget.

You will have to use a different method to get the result out of your python script most likely ProcessBuilder. There's a few examples on the forum of people doing this you can search for.

It's very possible your python script is running now but you don't think it is because your results are `None`.

> [@Procedure for installing python libraries from source](https://forum.inductiveautomation.com/t/procedure-for-installing-python-libraries-from-source/26022/16):
>
> Oh, right - system.util.execute is a 'fire-and-forget', it doesn't give you access to standard output. Try adapting this: from java.lang import ProcessBuilder, String pb = ProcessBuilder([r"C:\Users\pgriffith\AppData\Local\Microsoft\WindowsApps\python3.exe", r"C:\Users\pgriffith\Downloads\test.py"]) process = pb.start() output = String(process.getInputStream().readAllBytes()) print output (Note: This blocks until the process completes waiting on the input stream. It may or may not ever compl…

---

<div class="post-metadata">

**Author:** ![jrd](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jrd](https://forum.inductiveautomation.com/u/jrd)\
**Post date:** [December 8, 2023, 7:32pm UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876/7 "2023-12-08T19:32:53Z")

</div>

Thank you [bkarabinchak.psi]  
I am not relying on the return to determine whether the script fired. As you point out the doco explain that `system.util.execute` returns nothing. I do not require an results. The script reads and writes to a database. It works when fired from Visual Studio, but when called from Ignition the database is not modified

---

<div class="post-metadata">

**Author:** ![jrd](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jrd](https://forum.inductiveautomation.com/u/jrd)\
**Post date:** [December 8, 2023, 7:34pm UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876/8 "2023-12-08T19:34:54Z")

</div>

Thank you pturmel  
Is this different from Vision?

---

<div class="post-metadata">

**Author:** ![jrd](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jrd](https://forum.inductiveautomation.com/u/jrd)\
**Post date:** [December 8, 2023, 7:35pm UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876/9 "2023-12-08T19:35:47Z")

</div>

Hi [Hayden\_Watson]  
Where do I find this recommendation?

---

<div class="post-metadata">

**Author:** ![jrd](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@jrd](https://forum.inductiveautomation.com/u/jrd)\
**Post date:** [December 8, 2023, 7:37pm UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876/10 "2023-12-08T19:37:52Z")

</div>

victordcq  
I tried various methods to eliminate the backslash issue  
Can anyone clarify exactly this should be written, for Windows  
Thanks

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [December 8, 2023, 8:13pm UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876/11 "2023-12-08T20:13:03Z")

</div>

@jrd, to ping someone or have notification of a reply in their inbox hit the reply button under their post or use the `@username` syntax. I'm not sure if any of your previous four posts will have generated notifications. You can edit each with the pencil icon link.

---

<div class="post-metadata">

**Author:** ![Hayden\_Watson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hayden_watson/32/48792_2.png) [@Hayden\_Watson](https://forum.inductiveautomation.com/u/Hayden_Watson)\
**Post date:** [December 11, 2023, 8:29am UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876/12 "2023-12-11T08:29:48Z")

</div>

This is a good read, the flask method is in a section about running python 3 code:

> **[Python In Ignition](https://support.inductiveautomation.com/hc/en-us/articles/360056397252-Python-In-Ignition)**
>
> An Advanced Primer — Python, Libraries, Versions, and the Future
>  
> Background
> Ignition provides users with the ability to do Python scripting as a core part of the Ignition platform. Standard Editi...

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [December 11, 2023, 9:39am UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876/13 "2023-12-11T09:39:37Z")

</div>

in the docs they escape the escaped backslash

```auto
\ => \\

```

On your second var you used `r""` which also works  
So that is probably easier if you dont need any other escaped characters

---

<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:** [December 11, 2023, 1:00pm UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876/14 "2023-12-11T13:00:15Z")

</div>

Worth asking to get the simple thing out of the way - this path - this is the exact command you would run on the computer on that the ignition gateway is installed on? -`"C:\Users\Ross\AppData\Local\Programs\Python\Python311\python.exe", r"C:\Users\Ross\source\repos\PythonApplication1\PythonApplication1.py"`

You're running it on a gateway event script, so it's using the file system of the computer that the Ignition Gateway is installed on for one, and for two, then it would need permissions to the User\Ross folder if it doesn't have it. Perhaps all you need is to make sure the file path exists on the gateway or move it to a location the Ignition gateway has permissions for.

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [December 11, 2023, 3:13pm UTC](https://forum.inductiveautomation.com/t/run-remote-python-script-on-gateway/82876/15 "2023-12-11T15:13:19Z")

</div>

I asume the file is on the gateway, But the rights to the file is indeed something else to look for.

> [@jrd](#):
>
> a Python script on the gateway machine

> [@bkarabinchak.psi](#):
>
> Perhaps all you need is

Running an execute with unescaped `"\"` does not work, so that was definitly one thing wrong.
