Jan-11-2024, 09:34 AM
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 . Thank you!!!
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 . Thank you!!!