MY CRON SCRIPT WORKS BY HAND AND FAILS AT 3 AM WITH MODULENOTFOUNDERROR 🤔

Solvedcronpippythonvirtualenv5 replies · 61 views · started
Accepted solution by Oskar Varga — ahh classic classic case of pip and python being different interpreters , check: [code] The fix that always works is to install with the…
  1. im literally losing my mind here i might be wrong here, but Im losing my mind. I ran pip install requests, it said success. then I run my script:

    PS C:\scripts> python sync_reports.py

    PS C:\scripts> python sync_reports.py
    Traceback (most recent call last):
      File "C:\scripts\sync_reports.py", line 3, in <module>
        import requests
    ModuleNotFoundError: No module named 'requests'
    PS C:\scripts> pip install requests
    Requirement already satisfied: requests in c:\users\...\python311\lib\site-packages
    PS C:\scripts> python sync_reports.py
    ModuleNotFoundError: No module named 'requests'
    

    i ran pip install again and it says "Requirement already satisfied". so its installed AND not installed at the same time?

    1
  2. hmmm before anything else: cron runs with a minimal environment: short PATH, no shell profile, no activated venv, no pyenv/conda init. So python3 in cron may be the system interpreter while your terminal uses a different one, comparing which python3 in ur shell with what cron uses (log which python3 from a test cron line) usually shows it. hope that helps :)

    28 0 helpful
  3. Accepted solution

    ahh classic classic case of pip and python being different interpreters, check:

    which python python3 pip pip3
    python3 -c "import sys; print(sys.executable)"
    

    The fix that always works is to install with the exact interpreter you run:

    python3 -m pip install requests
    

    python -m pip guarantees teh package goes into the same environment that runs your script.

    2 6 helpful
  4. On Windows, the py launcher can pick a different instlal than python 🤷 use py -0p to list all installed interpreters, and install with py -3.x -m pip install requests for the one u actually run. worked for me anyway 🤷

    2 7 helpful
  5. @oskar.varga python3 -m pip install requests and it just works. ill never type bare pip again.

    make a backup first, just in case. u guys are the best!!!

    1 0 helpful

Reply to this topic

Sign in or create an account to reply.