# How to mport a python script

**URL:** https://discourse.nodered.org/t/how-to-mport-a-python-script/76626
**Category:** General
**Created:** [16 March 2023 12:08 UTC](https://discourse.nodered.org/t/how-to-mport-a-python-script/76626 "2023-03-16T12:08:20Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![beg1nner](https://avatars.discourse-cdn.com/v4/letter/b/d26b3c/32.png) [@beg1nner](https://discourse.nodered.org/u/beg1nner)
#### Post date: [16 March 2023 12:08 UTC](https://discourse.nodered.org/t/how-to-mport-a-python-script/76626/1 "2023-03-16T12:08:20Z")

</div>

Hi there.  
I want to use a whole python script I wrote (or "just" the functions) in node-red.

How to do that?

Tutorials I found were quite confusing.  
Thanks you for help.  
Your beg1nner

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [16 March 2023 12:47 UTC](https://discourse.nodered.org/t/how-to-mport-a-python-script/76626/2 "2023-03-16T12:47:05Z")

</div>

You can use an exec node to execute an external python script.  
There is also a python node for node-red.

Question is, do you really need python ? Most things can be done in javascript as well as both languages can do similar things.

---

<div class="post-metadata">

### Author: ![beg1nner](https://avatars.discourse-cdn.com/v4/letter/b/d26b3c/32.png) [@beg1nner](https://discourse.nodered.org/u/beg1nner)
#### Post date: [16 March 2023 12:52 UTC](https://discourse.nodered.org/t/how-to-mport-a-python-script/76626/3 "2023-03-16T12:52:02Z")

</div>

> [@bakman2](#):
>
> You can use an exec node to execute an external python script.  
> There is also a python node for node-red.

How to do it with the exec-node?  
And where can I find the python-specific-node?

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [16 March 2023 12:55 UTC](https://discourse.nodered.org/t/how-to-mport-a-python-script/76626/4 "2023-03-16T12:55:45Z")

</div>

Make sure to go through the documentation. [install nodes](https://nodered.org/docs/user-guide/editor/palette/manager)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/6/264bce750637920bfe3438154a90cfc5e385605b.png)

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [16 March 2023 12:57 UTC](https://discourse.nodered.org/t/how-to-mport-a-python-script/76626/5 "2023-03-16T12:57:41Z")

</div>

If your python script is an executable file you can run it with the exec node, passing any arguments in msg.payload.

If you prefer the python code to be stored inside your Node-red flow, here is an example showing how.

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/7/374f921a811da75ab5d5dc022c651398ca30dfeb.jpeg)

```auto
[{"id":"3f931e928d8bc3f3","type":"group","z":"db04fd79f3a93d19","name":"Executing a python script ","style":{"label":true},"nodes":["9761974cbd7f81d2","c4d009099ef709e3","20d8c64ff458227e","3b0d9776ffb8d484","3beba92f8eb5a185"],"x":14,"y":831.5,"w":832,"h":149.5},{"id":"9761974cbd7f81d2","type":"inject","z":"db04fd79f3a93d19","g":"3f931e928d8bc3f3","name":"Pass values in msg.payload","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"4.2 Python\\ script\\ stored\\ in\\ Node-red 99","payloadType":"str","x":180,"y":880,"wires":[["c4d009099ef709e3"]]},{"id":"c4d009099ef709e3","type":"template","z":"db04fd79f3a93d19","g":"3f931e928d8bc3f3","name":"Python script","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"python -c ' # -c argument - accept command inline. Must use single quotes.\nimport sys # Process argument list\nnum1 = float(sys.argv[1])\nstr = sys.argv[2]\nnum2 = float(sys.argv[3])\nprint (\"1st argument is \", num1)\nprint (\"2nd argument is \", str)\nprint (\"3rd argument is \", num2)\nprint()\nif(num1 <= 10):\n print (num1, \"is less than 10\")\nelse :\n print (num1, \"is greater than 10\")\nif (num2 <= num1):\n print (num2, \" is less than \", num1)\nelse :\n print (num2, \" is greater than \", num1)\n' {{payload}} # Closing single quote followed by list of arguments\n","output":"str","x":390,"y":880,"wires":[["20d8c64ff458227e","3b0d9776ffb8d484"]]},{"id":"20d8c64ff458227e","type":"exec","z":"db04fd79f3a93d19","g":"3f931e928d8bc3f3","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":550,"y":880,"wires":[["3beba92f8eb5a185"],["3beba92f8eb5a185"],[]]},{"id":"3b0d9776ffb8d484","type":"debug","z":"db04fd79f3a93d19","g":"3f931e928d8bc3f3","name":"Full Script","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":940,"wires":[]},{"id":"3beba92f8eb5a185","type":"debug","z":"db04fd79f3a93d19","g":"3f931e928d8bc3f3","name":"debug 299","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":880,"wires":[]}]

```

---

<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: [15 May 2023 12:58 UTC](https://discourse.nodered.org/t/how-to-mport-a-python-script/76626/6 "2023-05-15T12:58:12Z")

</div>

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