Python Forum
ModuleNotFoundError only if script is launched by cron
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ModuleNotFoundError only if script is launched by cron
#1
Hello

I want to launch a python program at start of the system, which uses openCV.
unfortunately, I get a ModuleNotFoundError on cv2 ONLY when the script is launched
by cron. Why ?

environment :
pi@raspberrypi:~ $ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 11 (bullseye)
Release: 11
Codename: bullseye

Python version :
pi@raspberrypi:~ $ ls /usr/bin/python*
/usr/bin/python /usr/bin/python3.9 /usr/bin/python3-config
/usr/bin/python3 /usr/bin/python3.9-config

opencv :
pi@raspberrypi:~ $ python3
>>> import cv2
>>> cv2.__version__
'4.5.4'

the automatic launch in Crontab :
@reboot sh /home/pi/lancement.sh > /home/pi/logs/log.txt 2>&1

lancement.sh
cd /home/pi/domoticz/scripts
/usr/bin/env python3.9 snap2.py&

If I launch lancement.sh manually, "import cv2" doesn't cause any error

and the program executes without any problem.
when cron calls lancement.sh, I get the error in log.txt :
Traceback (most recent call last):
File "/home/pi/domoticz/scripts/snap2.py", line 1, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'


Thank you for your help.
Reply
#2
Possibly your login environment has a PATH that points to one version of python and CRON is using the default system path that points to a different installation of python.

lancement.sh runs the python specified in the environment (the path) and that might be different between root and the "pi" account.

Try doing sudo su - and then try to run the script. Does it fail?
Reply
#3
Yes it does :
pi@raspberrypi:~ $ sudo su
root@raspberrypi:/home/pi# bash lancement.sh
root@raspberrypi:/home/pi# Traceback (most recent call last):
File "/home/pi/domoticz/scripts/snap2.py", line 1, in <module>
import cv2
ModuleNotFoundError: No module named 'cv2'

then I try in lancement.sh :
cd /home/pi/domoticz/scripts
/usr/bin/python3 snap2.py&

and it gives back the same error

then in lancement.sh :
cd /home/pi/domoticz/scripts
/usr/bin/python snap2.py&

and same again.

cv2 is there :
pi@raspberrypi:~/.local/lib/python3.9/site-packages $ ls
cv2 opencv_python-4.5.4.60.dist-info
keyboard __pycache__
keyboard-0.13.5.dist-info python_vlc-3.0.12118.dist-info
numpy vlc.py
numpy-1.21.5.dist-info

and PATH looks correct :
pi@raspberrypi:~ $ echo $PATH
/home/pi/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/home/pi/.local/lib/python3.9/site-packages

So what should I change ?
Reply
#4
As "pi" and as "root" run the following three commands and show the output. (I see your pi user PATH above, but I don't see root user PATH).

Output:
$ echo $PATH $ type python3.9 $ python3.9 -c 'import sys; print(sys.path)'
Reply
#5
pi@raspberrypi:~ $ echo $PATH
/home/pi/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/home/pi/.local/lib/python3.9/site-packages/cv2:/home/pi/.local/lib/python3.9/site-packages/cv2

pi@raspberrypi:~ $ type python3.9
python3.9 est /usr/bin/python3.9

pi@raspberrypi:~ $ python3.9 -c 'import sys; print(sys.path)'
['', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/home/pi/.local/lib/python3.9/site-packages', '/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.9/dist-packages']

pi@raspberrypi:~ $ sudo su


root@raspberrypi:/home/pi# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

root@raspberrypi:/home/pi# type python3.9

python3.9 est /usr/bin/python3.9

root@raspberrypi:/home/pi# python3.9 -c 'import sys; print(sys.path)'
['', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.9/dist-packages']

Big differences in PATHs, indeed. I added this line to .bashrc :
export PATH="$PATH:/home/pi/.local/lib/python3.9/site-packages/cv2"
but the PATH remains the same

Thanks a lot for your patience !
Reply
#6
The PATH doesn't seem that big a deal, both accounts are running "/usr/bin/python3.9". But the import path is different. The "pi" account has a .local site-packages that root isn't accessing.

I suppose something might be setting PYTHONPATH in your environment, but not in root.

Some options for you:

* Does it need to run as root? Could you put it in the "pi" user's crontab instead?
* Add "/home/pi/.local/lib/python3.9/site-packages" to PYTHONPATH in the root environment

(You could also have root install cv2 separately, but then you'd have two copies and a chance that they're not the same version. Better to keep one copy).
Reply
#7
(Feb-03-2022, 11:06 PM)bowlofred Wrote: The PATH doesn't seem that big a deal, both accounts are running "/usr/bin/python3.9". But the import path is different. The "pi" account has a .local site-packages that root isn't accessing.

I suppose something might be setting PYTHONPATH in your environment, but not in root.

Some options for you:

* Does it need to run as root? Could you put it in the "pi" user's crontab instead?
* Add "/home/pi/.local/lib/python3.9/site-packages" to PYTHONPATH in the root environment

(You could also have root install cv2 separately, but then you'd have two copies and a chance that they're not the same version. Better to keep one copy).

Thank you very much !
I could not succeed in adding PYTHONPATH in root environnment (why... If there is a good howto for that I take it !), but finally I put the crontab in the pi user and it works fine !
Reply
#8
Hi, I encountered similar questions and a few differences. Here's my problem:
Been trying to start a python script automatically every time the Raspberry Pi activated, tried every possible way(rc.local, bashrc, crontab and other ways), finally realize that the bashrc and crontab is the most suitable way for my utility.

But both ways return the same Error: Traceback (most recent call last): File "/home/fanshaoqi/chi/plant_phenotyping/Get_Plant_Architecture/0_Main_Script.py", line 2, in import CameraDistance File "/home/fanshaoqi/chi/plant_phenotyping/Get_Plant_Architecture/CameraDistance.py", line 1, in import cv2 ModuleNotFoundError: No module named 'cv2'

This is my .bashrc content: echo Running at boot sudo /usr/bin/python3 /home/fanshaoqi/chi/plant_phenotyping/Get_Plant_Architecture/0_Main_Script.py

This is my crontab content: PYTHONPATH=/home/fanshaoqi/.local/lib/python3.9/site-packages/cv2 LANG=nb_NO.UTF-8 LC_ALL=nb_NO.UTF-8 @reboot sudo /usr/bin/python3 /home/fanshaoqi/chi/plant_phenotyping/Get_Plant_Architecture/0_Main_Script.py

My Raspberry Pi information: fanshaoqi@raspberrypi:~ $ uname -a Linux raspberrypi 6.1.52-v8+ #1679 SMP PREEMPT Fri Sep 8 14:46:04 BST 2023 aarch64 GNU/Linux

fanshaoqi@raspberrypi:~ $ cat /proc/device-tree/model Raspberry Pi 4 Model B Rev 1.4

Pls can someone help me, been stuggling for days... Thank you!


AND I TRIED YOUR FIRST SUGGESTIONS, Try sudo su - and run the program it works okay.

and here's the information you mentioned:

fanshaoqi@raspberrypi:~/chi/plant_phenotyping/Get_Plant_Architecture $ ls /usr/bin/python*
/usr/bin/python /usr/bin/python3.9 /usr/bin/python3-config
/usr/bin/python3 /usr/bin/python3.9-config

fanshaoqi@raspberrypi:~/chi/plant_phenotyping/Get_Plant_Architecture $ python3
Python 3.9.2 (default, Mar 12 2021, 04:06:34)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.5.5'

fanshaoqi@raspberrypi:~/chi/plant_phenotyping/Get_Plant_Architecture $ echo $PATH
/home/fanshaoqi/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

fanshaoqi@raspberrypi:~/chi/plant_phenotyping/Get_Plant_Architecture $ type python3.9
python3.9 is /usr/bin/python3.9

fanshaoqi@raspberrypi:~/chi/plant_phenotyping/Get_Plant_Architecture $ python3.9 -c 'import sys; print(sys.path)'
['', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/home/fanshaoqi/.local/lib/python3.9/site-packages', '/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.9/dist-packages']

fanshaoqi@raspberrypi:~/chi/plant_phenotyping/Get_Plant_Architecture $ sudo su
root@raspberrypi:/home/fanshaoqi/chi/plant_phenotyping/Get_Plant_Architecture# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

root@raspberrypi:/home/fanshaoqi/chi/plant_phenotyping/Get_Plant_Architecture# type python3.9
python3.9 is /usr/bin/python3.9

root@raspberrypi:/home/fanshaoqi/chi/plant_phenotyping/Get_Plant_Architecture# python3.9 -c 'import sys; print(sys.path)'
['', '/home/fanshaoqi/chi/plant_phenotyping/Get_Plant_Architecture', '/home/fanshaoqi/.local/lib/python3.9/site-packages', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.9/dist-packages']

Really appreciate if someone can help Heart Heart . Thank you!!!
Reply
#9
Solved By reinstall the python-opencv via sudo pip install opencv-python, and new problem pop out and its fine, guess there are user permission or environmental problems, Thank you guys!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 937 Nov-16-2022, 07:58 PM
Last Post: Winfried
  script with imports works but pytest gives "ModuleNotFoundError"? Hpao 0 1,577 Jun-27-2021, 08:30 PM
Last Post: Hpao
  Python Script Spawned by Cron or Systemd doesn't write files..? johnnyrobot 2 2,597 May-24-2019, 07:04 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020