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
    Commented Aug 27, 2018 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
    Commented Aug 27, 2018 at 19:09
  • Exactly, it may work better, as Cron has affinity with bash commands
    – Redbob
    Commented Aug 28, 2018 at 15:33
  • Just tried, got the same error :( But thanks for your help
    – L. Faros
    Commented Aug 28, 2018 at 16:30

5 Answers 5


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.

  • 5
    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
    Commented Jan 15, 2019 at 13:30
  • 1
    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. Commented Jul 9, 2020 at 19:53

I had similar issues today, and here is how I solved it! Hope it'll help you.

Do you have two different python? Try to run:

where python3

If you have both /usr/local/bin/python3 and /usr/bin/python3 there, and your package is installed in /usr/local/lib but you're specifying to use /usr/bin/python3 in PYTHONPATH, it'll throw ModuleNotFoundError.

You can either:

  1. specify /usr/local/bin/python3 in your PYTHONPATH
  2. or run /usr/local/bin/python3 FILE in your cronjob.
  3. or put #!/usr/local/bin/python3 in the first line of your file

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?

  • 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
    Commented Aug 27, 2018 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
    Commented Aug 27, 2018 at 19:20

Your cron should be */5 * * * * /usr/bin/env python3 /home/louis/backup-server/main.py >> /var/log/MYbackup.log 2>&1

after /usr/bin/env give a space and mention python version


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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .