Search code examples
pythoncron

Library missing, GUI-python schedule execution using crontab


I wanted to run python program on raspberry pi at specified time using crontab but it gives an error. Running on command Terminal, I have no problem in executing the program.

Details Error:

Traceback (most recent call last):
  File "/home/pi/Desktop/Working_IonControllerVer3.py", line 6, in <module>
    import pyqtgraph as pg
ModuleNotFoundError: No module named 'pyqtgraph'

Terminal

$ sudo crontab -e 

39 5 * * * sh  /home/pi/Desktop/launcher.sh

shell script is as following

#! /bin/sh
export DISPLAY=:1
PATH=$PATH:/home/pi/.local/lib/python3.7/site-packages/pyqtgraph/
/usr/bin/python3.7 /home/pi/Desktop/codeVer3.py

Any suggestion how to fix this issue using crontab python program execution.


Solution

  • If pyqtgraph is not recognized globally then you should also be able to include the environment setting on the same line(in the same command) as the call to Python, like this as an example

    */1 * * * * PYTHONPATH=/Library/Frameworks/Python.framework/Versions/Current/lib/python2.7/site-packages python /Users/JohnDoe/Desktop/createUpdate.py
    

    Please be aware of spaces in the variable assignments. No semicolon and no need to export variables, since declaring them before the commands already make them active for the command itself.

    Following the example, set the pyqtgraph environment on the (command line) and it should solve your problem.