Python “no module named...” error when running SSH script from Shortcuts

I’ll preface this post with the caveat that both Python and all things Unix are new to me. I’ve been dabbling in some Raspberry Pi stuff and it is generally going well.

I’m having a nagging problem running a Python script from Shortcuts using the Run Script over SSH action that I can’t resolve.

The Python script I’m trying to run from Shortcuts executes fine when I run directly from the command line on the RPi. However, when I try to run the same script from Shortcuts, the script errors out with a “no module named gpizero” message.

The beginning of the Python script is:

from gpiozero import LED

The simple SSH action I’m using is:

I’m running a standard install of Raspbian Lite and Python 3. Everything is up to date. Let me know if there is other useful environmental info to help with troubleshooting.

I’ve exhausted my Googling and am going in circles. I would welcome any guidance from this group. Thanks in advance – jay

Finally figured it out. Posting here in case others trip on the same…

I needed to provide the full path to Python in the script


I suspect you’re not setting the path to Python 3 in the $PATH variable when you initiate the session.

Do a echo $PATH when you get a chance.

But your method would certainly work.

@Martin_Packer thanks for the tip. I assume that is the issue. This gives me more insight (and more things to learn). — jay


1 Like

While you can add python3 to your path, why not specify it after the shebang (#!...) on the first line of your script? That way you don’t depend on python3 being in the path.

from gpiozero import LED

In case you, or another future reader, don’t know about the shebang: it tells the operating system which interpreter should be used to execute the script. Thus, when a valid shebang exists on the first line, if no other interpreter is supplied by the user, the interpreter in the shebang will be used.

In the example above with the file named foo (Note that the .py extension is unnecessary), first make the script executable

chmod 755 foo

Then execute the file by simply typing its name: