"No module named 'torch'" using crontab

My Python code works fine if I run it from the command line by:

 $ python3 detect_people_main.py

Now I want to run the code from crontab using:

@reboot sleep 30 && /usr/bin/python3 /home/pip/Downloads/yolov7/detect_people_main.py >> /home/pip/Downloads/yolov7/logfile.log 2>&1 &

while printing the logs in logfile.log. But the code does not work from crontab. And the log is :

Traceback (most recent call last):
  File "/home/pip/Downloads/yolov7/detect_people_main.py", line 6, in <module>
    import torch
ModuleNotFoundError: No module named 'torch'

I am using Jetson NANO to run the code. Any suggestions to solve the issue?

I would guess your cron job might use another Python environment which does not have torch installed. You could check if e.g. which python3 (which seems to work) is pointing to another location than /usr/bin/python3.

1 Like

I was running the code on reboot using crontab from the sudo user. Somehow my Python from sudo user could not find Pytorch. After switching from sudo crontab -e to only crontab -e I solved the issue for Pytorch.