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:

#!/usr/bin/python
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

/usr/bin/python3 onair.py

1 Like

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

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

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.

#!/usr/bin/python3
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:

./foo

Hello, I am having the same issue that OP described but not able to solve it. I am using iOS shortcuts action run script over ssh but it says that module not found whereas I can run the same script without any issues directly on the raspberry pi. Any help is appreciated
Thank you

Have you looked at your path from Shortcuts to make sure your module is in it when you are accessing via Shortcuts?

which command should i use to know that ?
edit: ran echo $PATH from shortcuts and also the raspberry pi and they show different directories; on the pi these directories are shown : /home/pi/.local/bin:/home/pi/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
but from shortcuts only these are shown :
/usr/local/bin:/usr/bin:/bin:/usr/games

What can i do to resolve this ?

Set your PATH to have the paths as part of your SSH script from Shortcuts, or include it in the script.

The precise syntax could vary between shells I guess, but these should help.

tried including the command sudo export PATH=$PATH:~/dir/ as part of the script for SSH from Shortcuts but it says command not found

Maybe this?

Ok now got it to add the directories to path and checked with echo $PATH that they are added. But still having the same ModuleNotFoundError from shortcuts.

Check that you are signing in with the same user account in both options and that you can find the module on one of the PATH paths specified when you are signing in from Shortcuts.

if you have the module available and you have specified the appropriate oath as a place to look for it, then it should work.

Signing in with same user account. And yes the directory of the module is in the PATH variable when signing in with shortcuts (checked with echo $PATH).

you have specified the appropriate oath as a place to look for it

Did not understand what you mean by this

Sorry that was a typo. “oath” should have been “path”.

You basically need to make sure that your session can see the module.

Is there any other way I could explicitly mention the directory of the module in my script and use it?

EDIT : finally solved by including the directory of the modules directly in script by using sys.path.append('path/to/modules')