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
#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


Messages In This Thread
RE: ModuleNotFoundError only if script is launched by cron - by caicaifan - Jan-11-2024, 09:34 AM

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