Skip to content Skip to sidebar Skip to footer

Python Script Stops At Import Statement When Script Is Run As Cronjob

I'm executing a python script every 30 minutes with cron, but it stops at the import statement and I don't understand why because I don't get any feedback. In my crontab I have */3

Solution 1:

cron runs processes with a different environment to that of your terminal. Possibly you have set PYTHONPATH in your terminal, but not in your cron environment. If that is the case you can add it to your shell script:

export PYTHONPATH=/home/ziofil/python_scripts/whatever:$PYTHONPATHcd /home/ziofil/python_scripts
python script_30_mins.py

Solution 2:

I found out what the problem was. @UweMannl was right all along. After @mhawke pointed out that cron's environment is different than the one of my terminal, I thought that perhaps also the python binary could be different and indeed it was: I want /home/ziofil/anaconda3/bin/python and cron was using /usr/bin/python.

I modified the last line of the script to /home/ziofil/anaconda3/bin/python script_30_mins.py and everything works!

Post a Comment for "Python Script Stops At Import Statement When Script Is Run As Cronjob"