1

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!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
pmillerhk
  • 171
  • 1
  • 2
  • 12
  • Looks like you are using python3. Is that right? See my updated answer below. You need to specify the PATH in cron. – 9Breaker Oct 05 '17 at 12:32
  • Also since this is your first time using cron, you should know that you will not see any output in an open shell. The job is run in the background. If your script doesn't output a file, or create a log file, then your std out will be lost. If you are looking for verification that your script is running, you need to send the output to a log file. See https://superuser.com/questions/122246/how-can-i-view-results-of-my-cron-jobs – 9Breaker Oct 05 '17 at 12:41

3 Answers3

1

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)

9Breaker
  • 724
  • 6
  • 16
  • I must be missing something. I put that shebang in and changed the path to the full path of 20 9 * * * /Library/Frameworks/Python.framework/Versions/3.6/bin/python3/Users/lindseymiller/Desktop/Notify2.py and it still won't work. Wrong syntax? – pmillerhk Oct 05 '17 at 13:27
  • you would need a space between the two if you want to do it that way. yourLongPathToPython (make sure there is a space) yourPathToYourScript. – 9Breaker Oct 05 '17 at 13:36
  • You can also define the PATH variable if you do not want to type the full path to python3 (see the example above). That way bash will look in the folders specified in PATH and try to find python3. Either way should work, but if you choose to define PATH, then make sure you copy the spacing exactly (bash is particular with spaces when it comes to variable definitions) – 9Breaker Oct 05 '17 at 13:39
  • Also just to note, you do not need the shebang if you are doing it this way. When you call python3, you give it your script as input and it runs the script. Defining the shebang is useful when you make the script executable. But that is another thing. Let's try and get this working this way for you. – 9Breaker Oct 05 '17 at 13:41
  • still not working and I copied the syntax exactly: PATH=/Library/Frameworks/Python.framework/Versions/3.6/bin/python3 40 9 * * * python3 Users/lindseymiller/Desktop/Notify2.py Could it be that I wrote the .py script in Spyder? Does that prohibit me from running it this way? – pmillerhk Oct 05 '17 at 13:47
  • So specifying the PATH needs to be on a separate line in crontab. Also this is just the path to python3 so it needs to be: PATH=/Library/Frameworks/Python.framework/Versions/3.6/bin – 9Breaker Oct 05 '17 at 13:49
  • Then on a separate line below add your call to the job with the correct info for the time, followed by python3, and the full path to the file : 15 12 * * * python3 users/paul/desktop/pythonscript.py – 9Breaker Oct 05 '17 at 13:51
  • Maybe I am missing something earlier on ... I used Spyder to write a script then saved it to my desktop. Then I went to the terminal and input env EDITOR=nano crontab -e to get to crontab editor where I put in the syntax as you wrote it. Then control O to write out and enter to accept and contrl X to exit. The script should send me an email but the email never comes. It works if I run it manually inside spyder. Any thoughts? – pmillerhk Oct 05 '17 at 14:01
  • Ok, so yes there could be a difference with Spyder. You probably are running a different python with a different environment in Spyder. This can be confusing when you get multiple versions of python installed on your computer. I would see: https://unix.stackexchange.com/questions/300128/how-can-i-run-a-python-script-using-anaconda-from-the-command-line – 9Breaker Oct 05 '17 at 14:16
  • which python will only return the first instance of python it finds in PATH. There are probably multiple instances of python installed (I had this problem on my mac, the native version, the anaconda version, etc). – 9Breaker Oct 05 '17 at 14:18
  • Did you install spyder with anaconda? – 9Breaker Oct 05 '17 at 14:19
  • Also are you sure you want python3? Spyder could be configured for python 2. – 9Breaker Oct 05 '17 at 14:21
  • I separately installed python 2 and 3 (both) in addition to anaconda ... because I am a rookie :) so I just removed the python I installed and am trying again – pmillerhk Oct 05 '17 at 14:26
  • Ok, so yes, if you are using spyder with the anaconda environment, then you need to get that version and environment into crontab. – 9Breaker Oct 05 '17 at 14:30
  • Sorry ... what does that mean? Get that version and environment into crontab? – pmillerhk Oct 05 '17 at 14:32
  • So, I think the easiest way would be to get the path to your anaconda environment version of python (the one spyder is using). I would look here for help (the 2nd answer describes this) https://stackoverflow.com/questions/36365801/run-a-crontab-job-using-an-annaconda-env – 9Breaker Oct 05 '17 at 15:09
  • Also see this resource: https://conda.io/docs/user-guide/tasks/manage-environments.html Once you get the path to the anaconda environment version of python that you are using in spyder, then you can just add that path to PATH in crontab, but I would put it first in the list (right after PATH= ) – 9Breaker Oct 05 '17 at 15:15
0

First check whether your script contains the python shebang or not.

#!/usr/local/bin/python

above will the the path for your python enviornment and this will run just fine or change your cron syntax, and tell it to call python to run your file and

use your complete path for your file such as /home/users/.....

15 12 * * * python users/paul/desktop/pythonscript.py
faizan baig
  • 1,283
  • 15
  • 21
  • Thanks but it still won't work. I have this shebang in the python: #!/usr/bin/env python3 and the full path in the cron tab is python /Users/paul/desktop/pythonscript.py. Any idea what I'm doing wrong? – pmillerhk Oct 05 '17 at 12:17
  • Try adding the user to it 15 12 * * * root python /Users/paul/desktop/pythonscript.py – faizan baig Oct 05 '17 at 12:22
  • @pmillerhk I have a same need. can you please advise how you have solved this – sunny babau Apr 05 '22 at 17:10
0

Goal - I created python scripts(Scipt X) using Spyder(conda base),I want to schedule it in cron. Solution - I create one more script(script Y),written below code

prehand:- switch to conda env by typing below command in terminal

  1. conda activate //Switch to conda env
  2. which python //check the python version in my case(~anaconda3/bin/python)

Code below in script Y:

import os,import subprocess
subprocess.call("~anaconda3/bin/python <FULL SCRIPT X PATH>")

I call this (script Y) in crontab. Crontab entry

min hour date month week ~anaconda3/bin/python <full path to Script Y>

you are now ready to schedule spyder scripts in crontab.

Swapnil
  • 1
  • 1