ModuleNotFoundError: Problem with service on raspberry os

40 views Asked by At

I have a problem with my service running on a raspberry pi zero w. I made some python scripts and I remote develop with the Thonny ide. Everything works fine. The code is executable and I get no errors. When I made a service for those scripts (I wanted to start the scripts every time my zero w boots) and I got this error message:

Dec 14 10:32:38 pi python3[877]: File "/github/scripts/run.py", line 1, in <module>
Dec 14 10:32:38 pi python3[877]: from view import Display
Dec 14 10:32:38 pi python3[877]: File "/github/scripts/view.py", line 4, in <module>
Dec 14 10:32:38 pi python3[877]: from scanner import Scanner
Dec 14 10:32:38 pi python3[877]: File "/github/scripts/scanner.py", line 1, in <module>
Dec 14 10:32:38 pi python3[877]: import nmap
Dec 14 10:32:38 pi python3[877]: ModuleNotFoundError: No module named 'nmap'

But when I execute the script normally with "python3 [filename.py] it works and there is no ModuleNotFOundError message. I had a similar problem with "ModuleNotFoundError: No module name 'plutil'" , but when I updated pip3, it worked again. Somehow I don't know how to fix the problem with Nmap... I installed nmap with sudo apt install Nmap and I installed python3-nmap, so everything should be set up... I tried to uninstall nmap and install it again. I also tried different python env for the service start, but nothing works...

This is my service file:


[Unit]
Description=Service for Pi Zero Agent. Github:
After=multi-user.target
[Service]
Type=simple
ExecStart= /usr/bin/python3 /github/scripts/run.py
Restart=on-abort
[Install]
WantedBy=multi-user.target

I really hope somebody could help me. Thank you for every information that helps me!

1

There are 1 answers

0
hakw On

I have done similar things with Python and Raspberry Pi's. My solution has always been setting up a program-specific virtualenv in which I install all the required packages using pip, then point my systemd service file to use the virtualenv python executable rather than the system one.

I would end up with something like this (from memory):

ExecStart=/myprogram/venv/bin/python3 /myprogram/scripts/run.py

If you haven't used virtualenv before, do look it up. It is immensely useful (as opposed to globally installing Python packages using apt).