Pythonshell help

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:

python3 display_message.3 test

and test is displayed on the screen.

But that only works there.

Reading from

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:

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

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:

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 $ 

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 = “”

Have you tried something like this?

image

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)

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:

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

/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).

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.