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


Messages In This Thread
ModuleNotFoundError only if script is launched by cron - by Charles33 - Feb-03-2022, 05:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 983 Nov-16-2022, 07:58 PM
Last Post: Winfried
  script with imports works but pytest gives "ModuleNotFoundError"? Hpao 0 1,606 Jun-27-2021, 08:30 PM
Last Post: Hpao
  Python Script Spawned by Cron or Systemd doesn't write files..? johnnyrobot 2 2,644 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