Raspberry PI 4 - ModuleNotFoundError: No module named 'cv2' Error at Boot

Hi all,

I need help!
I create an Object Detection Project on a Raspberry PI 4.
When I start the potato_classify.py Script manually, everything works fine.

The final step is too start the potato_classify.py Script when the Raspberry PI boots.
Therefore I created the following crontab:

# Edit this file to introduce tasks to be run by cron.
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
@reboot python3 /home/pi/potato_classify.py /home/pi/potato_model.eim >/home/pi/potato_classify.log 2>&1 &

But then the following error occurred:

Traceback (most recent call last):
   File "/home/pi/potato_classify.py", line 3, in <module>
     import cv2
ModuleNotFoundError: No module named 'cv2'

What can I do? Where is the mistake?

Thanks in advance for the support,


The potato_classify.py script uses OpenCV as a dependency so type the following command in your terminal to install OpenCV: pip install opencv-python and then re-run the script.
That should solve the error! :slight_smile:

Hello @arijit_das_student,
thank you for your prompt reply.

OpenCV has to be installed, because if I run the script potato_classify.py manually as a pi user, everything works fine.
The error only occurs when starting the script via the crontab.

Can it be due to the different users? I think crontab is called as the root user.
Do the dependencies have to be installed in Python for each user, e.g. pip install opencv-python?

@CAPKE I think YES…you can access the root user terminal and then install OpenCV on it! Try it out and lemme know if it doesn’t work.

Hi @CAPKE Cronjobs don’t load your own environmental variables (very annoying) so e.g. PYTHONPATH environmental variable is not present, which can lead to this. You can look if it’s set via:


Then add the environmental variables in your crontab as described https://stackoverflow.com/a/10657111/107642.

If OpenCV is installed on your system but Python cannot find it, you will get the “no module named ‘cv2’” error message. This can happen if OpenCV is not in the Python path. The Python path is a list of directories where Python looks for modules and packages. You can check the Python path by running the following command in your terminal or command prompt:

python -c "import sys; print(sys.path)"

This will print out the directories in the Python path. Make sure that the directory where OpenCV is installed is included in the Python path.

1 Like