Scheduling a Python program on a 24/7 server

Keeping your computer on 24-7 is not practical, so if you want to execute a Python script at a particular time every day, you probably need a computer that is on all the time.

PythonAnywhere gives you access to such a 24-7 computer. You can upload a Python script and schedule it to run at a certain time every day. This availability can be useful, for example, when you want to extract some values (e.g., weather data) from a website and generate a text file with the value or other reports every day.

To schedule a Python script for execution on PythonAnywhere, follow these simple steps:

  1. Sign up for a free account at https://www.pythonanywhere.com.

  2. Go to your Dashboard, Files, Upload a File, and upload the Python file you want to schedule for execution.

  3. Go to Tasks and set the time of the day you want your script to be executed and type in the name of the Python file you  uploaded (e.g., myscript.py). Note that the time you enter should be in UTC.

  4. Click the Create button and you’re done.

Your Python file will now be executed every day at your specified time. If you don't have a Python script and you’re still confused about the benefit of this, here is a very simple Python script that you can  use to try the above steps:

If you don’t have a Python script and you’re still confused about the benefits of this PythonAnywhere feature, here is a very simple Python script you can use to schedule for execution:

from datetime import datetime
with open(datetime.now().strftime("%Y-%m-%d-%H-%M-%S"), "w") as myfile:
myfile.write("Hi there!")

The above code creates a text file and writes the string “Hi there!”  in that text file. The name of the text file will be the current date and time. For example one file name example would be 2018-02-16-18-20-33.txt.

That name is generated by datetime.now() indicating the date and time the script was executed.  Every time the script is executed, the script generates a new text file with a different name. You will have a new text file created every day.