I am running a little ubuntu server (version 18.04)

Before upgrading to 18.04 the cron was executing just fine.

This is my crontab (sudo crontab -e) :

*/5  *    * * *  PYTHONPATH=/usr/bin/python3 /usr/bin/python3 /home/louis/backup-server/main.py >> /var/log/MYbackup.log 2>&1

But when the cron start the following is logged :

Traceback (most recent call last):
  File "/home/louis/backup-server/main.py", line 7, in <module>
    import config.config as config
  File "/home/louis/python_helpers/config/config.py", line 2, in <module>
    from dotenv import load_dotenv
ModuleNotFoundError: No module named 'dotenv'

however if I run sudo python3 /home/louis/backup-server/main.py the script is correctly executed

I did run sudo pip install python-dotenv

Content of scripts :

# /home/louis/backup-server/main.py
# permissions : -rwxr-xr-x
import os
import requests
import json
import datetime
import sys
sys.path.insert(0, '/home/louis/python_helpers')
import config.config as config
import mail
import utils

def main():
    # some code

if __name__ == "__main__":


# /home/louis/python_helpers/config/config.py
# permissions : -rwxr-xr-x 
import os
from dotenv import load_dotenv
dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
# some config var

I do have an __init__.py file in /home/louis/python_helpers/config, let me know if for any other information I should add.

What bug me the most is that the scripts run just fine if started manually but not as cron.

Edit : I was unsure whether this question belongs here or on stackoverflow, so please tell me if I should move it (even though I don't know how beside creating a new question)

  • 1
    Have you ever put this command in a script and called it by crontab, instead? – Redbob Aug 27 '18 at 18:59
  • My apologies but I don't get what you're asking. What do you mean by "this command" ? Like I should create a .sh file running something like python3 script.py and calling this file in the cron ? – L. Faros Aug 27 '18 at 19:09
  • Exactly, it may work better, as Cron has affinity with bash commands – Redbob Aug 28 '18 at 15:33
  • Just tried, got the same error :( But thanks for your help – L. Faros Aug 28 '18 at 16:30

Well the issue was pretty dumb, I did run pip install python-dotenv and sudo pip install python-dotenv (the script need root permission to access some folder) but I didn't do sudo -H pip install python-dotenv.

After running this command, the cron execute just fine.

| improve this answer | |
  • 2
    same here. pip3 uninstall <packages> followed by sudo -H pip3 install <packages> got the cronjob running fine for me. But I'm wondering now if I should try virtual environment to avoid this problem entirely. – Nikhil VJ Jan 15 '19 at 13:30
  • pip3 uninstall <package> && sudo -H pip3 install <package> saved my life, but why? What is -H? --set-home? Why does that fix this issue? Thanks btw Faros and Nikhil. – ChrisDanger Jul 9 at 19:53

The issue is probably this part:


because PYTHONPATH should point to where your libraries are. What happens if you remove that part of the crontab?

| improve this answer | |
  • Well, at first I didn't have this part, but trying to figure things out I added it. However the result is exactly the same with or without that. I'll remove tho since it doesn't seems to be any helpfull – L. Faros Aug 27 '18 at 19:08
  • 1
    Just removed this part to be sure and the issue is still the same. Thanks for your help though – L. Faros Aug 27 '18 at 19:20

for every user which you use for cron jobs, you should do pip install your-library. Because cron uses the environment whose user run the job.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.