Search code examples
pythoncronanacondaschedulingspyder

Using Cron to run a python script written in Spyder IDE


I have written my first python script using Spyder as the IDE within Anaconda. Yay! The script pulls data from google finance and emails it to me. When run inside Spyder it works fine.

Now I want to schedule that script to run at a specific time during the day. So, after researching, I have tried to set up the job to run in Cron with the following syntax:

15 12 * * * users/paul/desktop/pythonscript.py

I thought this would run the script at 12:15 but nothing happens.

I tried experimenting by opening the script in IDLE and running it or running it from a terminal but I can't get any of those to work because it tells me that none of the modules are imported.

So...can anybody tell me how to schedule a python script that was written in Spyder to run at a specific time?

Thanks!


Solution

  • You will need to specify the PATH variable within Cron and make sure python3 is in it. (It looks as from your comments you are using python3, make sure you know if you are using 2 or 3, just typing python will usually default to python 2) You can make normal edits like this with:

    crontab -e 
    

    Then add the full path to python before your call to your job. EDIT: This path needs to be the path to your anaconda environment python (to avoid compatibility issues between other versions of python on your system).

    PATH=path/to/anaconda/env/bin #you need to look this up
    
    15 12 * * * python3 users/paul/desktop/pythonscript.py
    

    See How to get CRON to call in the correct PATHs

    If you don't include the folder that contains the anaconda environment python3 in your PATH, it will not run exactly like it does in spyder. If you would like to know where anaconda version of python is type this in bash:

    conda info --envs
    conda env list #or you could try this
    

    If your command includes the call to your anaconda environment python, then you do not need the shebang in pythonscript.py. If you don't want to include the call to python in your command in crontab, then include the shebang in your python script on the first line.

    These may be useful: run a crontab job using an annaconda env (see the second answer there)

    https://conda.io/docs/user-guide/tasks/manage-environments.html (a guide for managing conda environments)