prwiley
Posts: 20
Joined: Tue Jun 19, 2018 2:08 pm
Location: Pennsylvania USA

Trouble starting script with systemd: ModuleNotFoundError

Sun May 09, 2021 2:34 pm

I have a python program that I want to run at startup and I have configured systemd to make this happen.

The program runs just fine from inside Thonny. I think systemd is configured properly because it does attempt to run the program on reboot

However, when systemd attempts to start the program it fails with a "ModuleNotFoundError: No module named paho". Since the program runs in Thonny I know paho is available. I presume there is some path setting I have to update to get it to work with systemd, but I don't know enough about it to look . . .

Can someone please explain what might be wrong and point me in the right direction. Many thanks.

User avatar
Paeryn
Posts: 3275
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Trouble starting script with systemd: ModuleNotFoundError

Sun May 09, 2021 2:57 pm

First guess would be that you are running your program with the wrong python interpreter. Thonny uses python3, if you are trying to run your program with python (which is python2) then it won't see anything that is only installed for python3 and might not work properly anyway as there are a few key incompatable differences between python2 and python3.

If the correct interpreter is being used then it could be down to where the module was installed or is. If you install a module just for a user rather than system-wide and then try to run a program as root (or any other user) then the module won't be found.
She who travels light — forgot something.
Please note that my name doesn't start with the @ character so can people please stop writing it as if it does!

MiscBits
Posts: 249
Joined: Wed Jan 27, 2021 12:48 pm

Re: Trouble starting script with systemd: ModuleNotFoundError

Sun May 09, 2021 3:22 pm

If I remember correctly Thonny uses a virtual environment for modules and its possible the module is not globally loaded.

You may need to reinstall paho-mqtt with

Code: Select all

sudo pip3 install paho-mqtt
to make it available to all.
Is a computer language with goto's totally Wirth-less?

prwiley
Posts: 20
Joined: Tue Jun 19, 2018 2:08 pm
Location: Pennsylvania USA

Re: Trouble starting script with systemd: ModuleNotFoundError

Sun May 09, 2021 3:38 pm

Thanks.

I am aware of the compiler issue, so my ExecStart=/usr/bin/python3

Reinstalling paho-mqtt as suggested did the trick to the extent that it got me past the ModuleNotFoundError . . .

However now I have a new problem. The program fails

Code: Select all

May 09 16:26:40 margo python3[623]:     sock = self._create_socket_connection()
May 09 16:26:40 margo python3[623]:   File "/usr/local/lib/python3.7/dist-packages/paho/mqtt/client.py", line 3546, in _create_socket_conn
May 09 16:26:40 margo python3[623]:     return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
May 09 16:26:40 margo python3[623]:   File "/usr/lib/python3.7/socket.py", line 707, in create_connection
May 09 16:26:40 margo python3[623]:     for res in getaddrinfo(host, port, 0, SOCK_STREAM):
May 09 16:26:40 margo python3[623]:   File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
May 09 16:26:40 margo python3[623]:     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
May 09 16:26:40 margo python3[623]: socket.gaierror: [Errno -2] Name or service not known
May 09 16:26:40 margo systemd[1]: selfiemachine.service: Main process exited, code=exited, status=1/FAILURE
May 09 16:26:40 margo systemd[1]: selfiemachine.service: Failed with result 'exit-code'.

prwiley
Posts: 20
Joined: Tue Jun 19, 2018 2:08 pm
Location: Pennsylvania USA

Re: Trouble starting script with systemd: ModuleNotFoundError

Sun May 09, 2021 5:09 pm

After some thought, I figure th error is a result of the network not being available when the script attempts to run. So I changed the systemd Unit file to wait for the network to be ready and also to try to restart the service in case it was not.

As described here: https://ma.ttias.be/auto-restart-crashe ... e-systemd/

Return to “Python”