0

My python script imports cv2, and it works fine when I start the script manually.

If I set the script to launch on boot (either via crontab, or via a .desktop file (in the user/.config/autostart/whatever.desktop) I get error "cv2 module not found".

I suppose the script is run in two different environments then, one that ha the cv2 module installed when I start the script manually, and in another environment that doesn't have the cv2 module installed when it is triggered automatically.

How can I rectify this?

Either have the script autorun in the same environment as when I manually trigger it, or install the applicable modules in that environment where it runs when autotriggered.

The contents of the .desktop file is

[Desktop Entry]
Exec=bash /home/RecorderV1/Recorder/launcher.sh

The launcher.sh script looks like this:

#!/bin/bash
cd /home/RecorderV1/Recorder/
/usr/bin/python3 main.py > logs/bashlog.txt 2>&1

When starting from crontab (sudo crontab -e) I have this line:

@reboot sh /home/RecorderV1/Recorder/launcher.sh >/home/RecorderV1/Recorder/logs/cronlog 2>&1

I have installed cv2 with

pip install opencv-python

And like I said it works fine when I open the .py file and run it manually.

1
  • 1
    The fundamental problem with this question (like hundreds of others) is that you are attempting to run a mysterious unlisted script.
    – Milliways
    Sep 13, 2022 at 9:34

2 Answers 2

1

If you install as a user ONLY that user has access.

Install with sudo.

Frankly installing with pip is a poor practice use sudo apt install python3-opencv to install the version in the repository.

0
0

Encountering the same question. 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!

2
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
    – Milliways
    Jan 11 at 9:44
  • Thank you for your reply. I'll do what you told. 👍
    – Chi
    Jan 12 at 0:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.