I have uninstalled Python2 on my Raspberry Pi and I am using Python3 only. I have an alias for python=python3 in place, which works in most cases. However, I noticed that the GPIO node no longer works as the underlying Python seems to want Python2.
I altered the GPIO node as follows to get it working again:
Open file .../node_modules/node-red-node-pi-gpio/nrgpio and change the last line to python3 -u $BASEDIR/nrgpio.py $@
Open file .../node_modules/node-red-node-pi-gpio/nrgpio.py and change the first line to #!/usr/bin/python3
Open file .../node_modules/node-red-node-pi-gpio/testgpio.py and change the first line to #!/usr/bin/python3
I'm hoping that as the transition to Python3 moves foreword, this will become a standard practice for nodes that have underlying Python engines. At the very least, it would be nice if the GPIO javascript file would fall back to another Python script that calls out Python3 in the shebang.
Yes - in fact there is a beta version I would like to get feedback on
If you go into your ~/.node-red dir and run npm i node-red-node-pi-gpio@2.0.0-beta3 that should grab the latest and you will see the nrgpio file now tries python3 first but falls back to python if not available.
Thanks for the response! I did give that an install but I'm still seeing the node report 'not available'.
I think this is because the top line of both the file nrgpio.py and testgpio.py is #!/usr/bin/python. If I change that top line to say instead #!/usr/bin/python3 then it works. I'm not sure how to make that fall back to Python2 however. There might need to be two identical scripts, just with different shebangs. Or, now that we're past the sunset date on Python2, just use the one shebang #!/usr/bin/python3 in the next version of the node?
...or the javascript could take the feedback from the shell script nrgpio to determine which Python is installed, and then in the javascript file itself just call out child.spawn("/usr/bin/python3 " + pythonScript) or child.spawn("/usr/bin/python " + pythonScript) respectively. Then you could completely eliminate the shebang in the python files and let the javascript force the interpretation?
the js file calls the nrgpio file that then calls the nrgpio.py file - as the shell script explicitly calls the python or python3 then the fact that the .py file has the shebang should be irrelevant. (so yes it could be removed - but does no harm)
Oh I see. Then the same just needs to be done inside the js file for testgpio.py on line 12. Running execSync(__dirname+'/testgpio.py'); yields the same problem of defaulting to Python2 I believe. Also I think the program would fail on lines 228 and 279 of the js file where the python is called directly like node.child = spawn(gpioCommand+".py",...
Yup I agree. It's just a feature request and I appreciate your consideration. Looks like @dceejay is already halfway there though and I'm happy to see you're already anticipating these kinds of requests
but yeah - I can't understand why they insist on leaving it called python3... if it is the preferred/supported version it should be aliased to python already... and leave python2 for any that really need it.
Thank you very much @dceejay , I have completed some tests with node-red-node-pi-gpio@next and it is working perfectly. I really appreciate your time on this, keep me updated for when it leaves beta!