# Can't execute Python script via Node-RED

**URL:** <https://discourse.nodered.org/t/cant-execute-python-script-via-node-red/67764>\
**Category:** General\
**Created:** [15 September 2022 19:36 UTC](https://discourse.nodered.org/t/cant-execute-python-script-via-node-red/67764 "2022-09-15T19:36:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PedroFilipe](https://avatars.discourse-cdn.com/v4/letter/p/3da27b/32.png) [@PedroFilipe](https://discourse.nodered.org/u/PedroFilipe)\
**Post date:** [15 September 2022 19:36 UTC](https://discourse.nodered.org/t/cant-execute-python-script-via-node-red/67764/1 "2022-09-15T19:36:59Z")

</div>

I'm trying to execute a python script through Node-RED, and while previously it worked, I had to format my Raspberry Pi and now it no longers works.

The errors it outputs is "cannot connect to X server". The script works fine if I execute it in a Raspberry terminal, and when I reinstalled Node-RED I imported my previous flow, so everything is exactly as it was before.

Here is a picture with the exec node configuration.  
 ![Capturar](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/8/f83ff6a9efe051dcdcc8744217e0ebcf4c31647d.png)

Is there any configuration on the Raspberry Pi I might have missed out while installing it again or is the problem coming from Node-RED?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 September 2022 07:42 UTC](https://discourse.nodered.org/t/cant-execute-python-script-via-node-red/67764/2 "2022-09-16T07:42:29Z")

</div>

Does you script work if you SSH in from another machine and run it there?

Does your script expect there to be a display?

Try using  
`DISPLAY=:0 python....`  
or  
=:1

If still no joy then show us the full error message. Is that error coming from the error output on the exec node?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 September 2022 08:39 UTC](https://discourse.nodered.org/t/cant-execute-python-script-via-node-red/67764/3 "2022-09-16T08:39:28Z")

</div>

> [@PedroFilipe](#):
>
> "cannot connect to X server"

From a [quick search on internet](https://stackoverflow.com/questions/39840184/python-code-crashes-with-cannot-connect-to-x-server-when-detaching-sshtmux-se)...

> ...means that your code is trying (and failing) to connect to an X server -- a GUI environment -- presumably being forwarded over your SSH session.

> If you want to stop it from being able to make any GUI connection at all (and perhaps, if the software is thusly written, from even trying), unset the `DISPLAY` environment variable before running your code.

---

<div class="post-metadata">

**Author:** ![PedroFilipe](https://avatars.discourse-cdn.com/v4/letter/p/3da27b/32.png) [@PedroFilipe](https://discourse.nodered.org/u/PedroFilipe)\
**Post date:** [16 September 2022 14:05 UTC](https://discourse.nodered.org/t/cant-execute-python-script-via-node-red/67764/4 "2022-09-16T14:05:30Z")

</div>

My script is a QR reader that uses a camera module to scan qr codes in the field of view, the script shows a window that displays the cameras field of view.  
Using the "DISPLAY=:0" allows the script to run but it no longer functions as before, previously when it detected a QR code it would output the QR code contents in Node-RED, but using "DISPLAY=:0" it doesn't output the QR code content.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/0/e093da1aa20308d99a8f155160ef8bcc508ae352.png)

The error comes out of the stderror exec output.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 September 2022 15:26 UTC](https://discourse.nodered.org/t/cant-execute-python-script-via-node-red/67764/5 "2022-09-16T15:26:49Z")

</div>

> [@PedroFilipe](#):
>
> previously when it detected a QR code it would output the QR code contents in Node-RED, but using "DISPLAY=:0" it doesn't output the QR code content.

Does your script send it to stdout?

---

<div class="post-metadata">

**Author:** ![PedroFilipe](https://avatars.discourse-cdn.com/v4/letter/p/3da27b/32.png) [@PedroFilipe](https://discourse.nodered.org/u/PedroFilipe)\
**Post date:** [18 September 2022 14:11 UTC](https://discourse.nodered.org/t/cant-execute-python-script-via-node-red/67764/6 "2022-09-18T14:11:50Z")

</div>

Yeah it does, but I fixed the issue by importing an older version of my flow. Maybe something went wrong when importing since I didn't have everything installed yet.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [17 November 2022 14:11 UTC](https://discourse.nodered.org/t/cant-execute-python-script-via-node-red/67764/7 "2022-11-17T14:11:52Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
