Search code examples
pythonpython-3.xcron

"ImportError: No module named requests" in crontab Python script


I'm trying to set up a Python script in crontab. I have the following:

0-59 * * * * python ~/PycharmProjects/dailySearch.py trees >> ~/woah.log 2>&1

In my log file I'm getting ImportError: No module named requests. I've scoured other SO questions and tried various suggestions with no success.

When I run the script in my IDLE, sys.version shows my version as 3.7.1 (default, Dec 14 2018, 13:28:58)

I have two Python versions installed and though I'm running python3 (3.7.1), I'm not sure that my I have requests and pip installed where it should be as well

python3 is at: /Users/jaai/anaconda3/bin/python3

pip3: /Users/jaai/anaconda3/bin/pip3

And to confirm I'm installing requests using pip3, I ran sudo pip3 install requests with the following:

Requirement already satisfied: requests in /Users/jaai/anaconda3/lib/python3.7/site-packages (2.22.0)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /Users/jaai/anaconda3/lib/python3.7/site-packages (from requests) (1.24.1)
Requirement already satisfied: certifi>=2017.4.17 in /Users/jaai/anaconda3/lib/python3.7/site-packages (from requests) (2018.11.29)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Users/jaai/anaconda3/lib/python3.7/site-packages (from requests) (3.0.4)
Requirement already satisfied: idna<2.9,>=2.5 in /Users/jaai/anaconda3/lib/python3.7/site-packages (from requests) (2.8)

Any help is appreciated!


Solution

  • A couple of things could be the culprit, but just to make sure the interpreter you run is the same cron is using, make sure you include the full path to python3, i.e.

    0-59 * * * * /Users/jaai/anaconda3/bin/python3/python ~/PycharmProjects/dailySearch.py trees >> ~/woah.log 2>&1
    

    If the problem persists then maybe you should look into setting the PYTHONPATH environment variable for cron so that it matches the one set for your shell.