Wednesday, March 16, 2022

[SOLVED] Crontab | Missing Python Module

Issue

I'm very new to Unix and crontab. The only major issue I'm running into is pointing terminal to the python modules for the specific program I'm trying to run. From command line the program runs fine but won't from crontab.

The first cronjob sends me an email saying that the cronjob is running. The second(createUpdate) runs a script I've built, set to run each minute.

crontab -l returns:

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

Am I structuring PYTHONPATH correctly?

Should I break it out before the cron?

Is 'export PYTHONPATH' necessary?

EDIT I forgot to add the error

/bin/sh: PYTHONPATH: command not found
Traceback (most recent call last):
  File "/Users/JohnDoe/Desktop/createUpdate.py", line 1, in <module>
    import beatbox
ImportError: No module named beatbox

Solution

The right way would be

*/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 variable assignment. No semicolon and no need to export variables, since declaring them before the commands already makes them active for the command itself.



Answered By - user3545605
Answer Checked By - Marilyn (WPSolving Volunteer)