# Pythonshell help

**URL:** <https://discourse.nodered.org/t/pythonshell-help/844>\
**Category:** General\
**Created:** [16 June 2018 01:30 UTC](https://discourse.nodered.org/t/pythonshell-help/844 "2018-06-16T01:30:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [16 June 2018 01:30 UTC](https://discourse.nodered.org/t/pythonshell-help/844/1 "2018-06-16T01:30:57Z")

</div>

I am trying to get pythonshell working.

I am wanting to run a python script on a remote machine to display a message on the LCD screen.

On that machine I run the ..... python script eg:

```auto
python3 display_message.3 test

```

and test is displayed on the screen.

But that only works there.

Reading from

> **[node-red-contrib-pythonshell](https://flows.nodered.org/node/node-red-contrib-pythonshell)**
>
> nodes used to interact with python processes

Executing a python script from node-red. Input to the node will become the argument for the python script,

So if I put this flow on the machine with the LCD display and the python script:

```auto
[{"id":"904a5c2c.441b28","type":"inject","z":"e9882c4e.450a6","name":"","topic":"","payload":"Banana","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":220,"wires":[["9ca2d2c2.38037","fbd23ff1.bf0da"]]},{"id":"fbd23ff1.bf0da","type":"pythonshell in","z":"e9882c4e.450a6","name":"Display message on LCD","pyfile":"/home/pi/PiFace/message_display3.py","virtualenv":"","continuous":false,"x":510,"y":220,"wires":[["1bf9aa39.20fa36"]]}]

```

I would take it as I would get "banana" on the screen.

Doesn't happen. But I'm sending banana as the "input to the node" which - as stated above - will become the arguement to the python script.

This is the error I get:

```auto
exit code: 1, Traceback (most recent call last): File "message_display3.py", line 4, in <module> import pifacecad File "/home/pi/PiFace/pifacecad/ __init__.py", line 44, in <module> from .ir import ( File "/home/pi/PiFace/pifacecad/ir.py", line 4, in <module> import lircImportError: No module named lirc

```

(Though after looking at it, I think there is another nasty happening.)

Stuck.

Thanks.

Late addition:  
This is the script:

```auto
cat message_display3.py 
#!/usr/bin/python
 
import sys
import pifacecad
 
#print('Arguments:', len(sys.argv))
#print('List:', str(sys.argv))
 
#if sys.argv < 2:
# print('To few arguments, please specify a filename')
 
filename = sys.argv[1]
print('The paramater you entered is called')
print( filename)

cad = pifacecad.PiFaceCAD()
cad.lcd.backlight_on()
cad.lcd.clear()
cad.lcd.write(filename)
pi@PIFACE:~/PiFace $ 

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [16 June 2018 02:45 UTC](https://discourse.nodered.org/t/pythonshell-help/844/2 "2018-06-16T02:45:43Z")

</div>

Working on the above problem:

I am looking at msg.payload.

I’m putting it through a switch.node.

If the message is a message I want to send it to output 1.  
If it is a “” I want to send it to output 2.

I can’t work out how to detect a length of 0.  
If I was using a function.node, no problems. (Which I may do now) but for the sake of learning:

In the switch.node how do I detect a msg.payload = “”

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [16 June 2018 04:39 UTC](https://discourse.nodered.org/t/pythonshell-help/844/3 "2018-06-16T04:39:19Z")

</div>

Have you tried something like this?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/2b82956e52ef2e390908a63b1c7cbdce9c487afd.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [16 June 2018 05:15 UTC](https://discourse.nodered.org/t/pythonshell-help/844/4 "2018-06-16T05:15:12Z")

</div>

Thanks,

I think I did. But I goofed and put “” in the first one.

Then the second one wasn’t quite as you had it, but more the “otherwise…” option.

As I couldn’t get the syntax correct, I went with the other way of using a function node and split them what way.

The next big problem is if there is more than one “word” in the parameter/s.

Oh and what is the latest problem is that my script (which I got working - a slightly long way) only accepts ONE word as the message. I don’t know how to make it that it accepts a “sentence” of many words.

It is something to do with the [1] in the filename = sys.argv[1].

Dunno if I can make a loop for as many [] as there are - beyond my knowledge at this point or what…

(Dumb look)

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [16 June 2018 16:45 UTC](https://discourse.nodered.org/t/pythonshell-help/844/5 "2018-06-16T16:45:23Z")

</div>

The “otherwise” check for port #2 should be no different in behavior, so that’s fine.

The `argv` array holds the command itself (as `argv[0]`) followed by all the command line arguments, taken individually as delimited by whitespace – so if you invoke this:

```auto
/usr/local/bin/myscript.py word1 word2

```

you get the argv array `["/usr/local/bin/myscript.py", "word1", "word2"]`. But if you want to pass all of those words together as 1 argument, just surround them with quotes:

```auto
/usr/local/bin/myscript.py "word1 word2 word3"`

```

This way, your script will get as array of 2 strings `["/usr/local/bin/myscript.py", "word1 word2 word3"]`. Of course, you will then have to split up the `argv[1]` “sentence” on whitespace, if you want to check for each of the words. Probably simpler to just let the command line do the splitting, and loop through the `argv` array in your python code, starting with index=1 (sorry, but I don’t know python, so I don’t know that syntax).

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [16 June 2018 22:28 UTC](https://discourse.nodered.org/t/pythonshell-help/844/6 "2018-06-16T22:28:54Z")

</div>

Thanks.

Yes, I get the argv[0] is the command. [1] is the first word after that, and so on.

As putting quotes around the message achieves the desired result, I shall probably leave it at that.  
Stripping the quotes on the machine (locally) will be too much trouble for what it is worth.

Sorry but your references to:

> /usr/local/bin/myscript.py word1 word2

Um, Ok, that is back at how it sees the array when it gets the command - right?

> you get the argv array ["/usr/local/bin/myscript.py", "word1", "word2"]

Above me. I don't understand what that means.

It would seem we are both new to python(3). No worries. Gotta start somewhere.

Anyway, as it is working now, I shall leave well enough alone and continue attacking the other things that have me stuck.

Appreciated the help though.
