How to schedule a cron on pythonanywhere server

If you have hosted your Django app on server and you want to schedule a cron job on the server to execute some task periodically ( daily or hourly) then this article is for you.

Cron jobs are executed after a fixed interval of time, repetitively until stopped explicitly. This may come handy if you want to send bulk mails every hour or you may want to take a backup of your database or basically anything you want to perform periodically.

Creating and hosting Django app:

In one of our previous article we discussed how to create a django application in less than 5 minute. This article contain step by step process to develop django app complemented by code on github.

In another related article we discussed how to host a django app on server for free. In this article we listed benefits of pythonanywhere server and steps to host the app in details.  

Article consist of screenshots to help you and a video tutorial showing working of article steps.

How to schedule cron on pythonanywhere server:

      Please comment if you have any query.  Create a free account on pythonanywhere with 30 days money back guarantee on paid account. Pay monthly.

      First make sure you have created your custom management command which will perform the task. e.g. sending emails or creating backup etc.

      First run this command from terminal and check if it is working as expected or not.

      Go to schedule tab in your account on pythonanywhere server.

      how to schedule a cron on pythonanywhere server

      As you can see that you can schedule a cron on daily basis. Hourly basis scheduling is available to paid users only.

      Time is given in UTC format.

      Select the hours and minutes accordingly and in command path input field enter the complete command with below format.
      absolute-path-to-python  command_name e.g.

      /home/100rupeekart/VirtualEnv/bin/python3.4   /home/code108/myproject/  send_mail_command

      Add the command. To see the output of the command later on, click on the command in list of scheduled commands.

      Python Script 1: Convert ebooks from epub to mobi format

      We are starting a series of python scripts which we may use in our daily life to automate mundane task and save some time.

      This is the first article in this series. Recently I bought Amazon's Ebook Reader, kindle paperwhite 3.

      I purchased few books from kindle store and downloaded most of the books in Epub format. Now kindle doesn't support epub format. You need to convert them to either mobi or azw3 format.

      Converting books one by one using some online tool is extremely time consuming and frustrating. Hence I searched for some tool which might perform bulk conversion. I found calibre. You may install and use it to convert books from one format to other in bulk.

      But if you are more of a terminal guy then you need to read further. First install the calibre. Use the tool if you want to. Or use the below script to convert the books.

      from os import listdir, rename
      from os.path import isfile, join
      import subprocess
      # return name of file to be kept after conversion.
      # we are just changing the extension. azw3 here.
      def get_final_filename(f):
          f = f.split(".")
          filename = ".".join(f[0:-1])
          processed_file_name = filename+".azw3"
          return processed_file_name
      # return file extension. pdf or epub or mobi
      def get_file_extension(f):
          return f.split(".")[-1]
      # list of extensions that needs to be ignored.
      ignored_extensions = ["pdf"]
      # here all the downloaded files are kept
      mypath = "/home/user/Downloads/ebooks/"
      # path where converted files are stored
      mypath_converted = "/home/user/Downloads/ebooks/kindle/"
      # path where processed files will be moved to, clearing the downloaded folder
      mypath_processed = "/home/user/Downloads/ebooks/processed/"
      raw_files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
      converted_files =  [f for f in listdir(mypath_converted) if isfile(join(mypath_converted, f))]
      for f in raw_files:
          final_file_name = get_final_filename(f)
          extension = get_file_extension(f)
          if final_file_name not in converted_files and extension not in ignored_extensions:
              print("Converting : "+f)
                  s = rename(mypath+f, mypath_processed+f)
              except Exception as e:
              print("Already exists : "+final_file_name)

      I have a folder 'ebooks' which contains all the downloaded ebooks.

      After files are converted to required format, they are stored in 'ebooks/kindle' directory and the original file is moved to 'ebooks/processed' directory.

      Once calibre is installed, a command line utility 'ebook-convert' is made available which takes two command line arguments, name of file to be converted and name of output file. We will be calling this command line utility from our program, passing the file names one by one.

      As of now we are ignoring pdf file as they take lot of time to convert and require some setting updates in calibre.

      I will leave that up to you.

      Kindle unlimited offer : Read over 1 million books.

      Virtual Environment in Python - A Pocket Guide

      In almost every article, we recommended the use of virtual environment for developing any Python or Django project.

      In this article, we will briefly cover the virtual environment in python, installation and usage.

      What is a Virtual Environment:

      Virtual environment is an isolated python environment which can be created using virtualenv python tool. This virtual environment contains all the packages that a python package would require. 

      Python project running in virtual environment does not use the system wide installed python package.  

      Installing Virtual Environment:

      We can install virtual environment using pip.

      pip install virtualenv

      You can confirm the installation by running below command.

      virtualenv --version
      rana@Brahma ~$ virtualenv --version

      Using virtual environment:

      Create a virtual environment:

      virtualenv project_1_venv

      This will create a virtual environment with name project_1_env  and install python binaries in this folder. This will use the system's default python' version.

      To create virtual environment with specific python version use below command.

      virtualenv -p /usr/bin/python2.7 venv2

      virtualenv -p /usr/bin/python3 venv3

      To work inside a virtual environment, we need to activate it.

      source virtual_env_folder/bin/activate

      Now you may install python packages inside this isolated environment using pip install command.

      Once you are done working with your project, you may deactivate the virtual environment by deactivate  command.   

      Deleting virtual environment:

      To delete a virtual environment simply remove the directory.

      rm -rf virtual_env_directory

      Freezing the requirements:

      When you deploy your project on any other server like PythonAnyWhere, you need to replicate the virtual environment.

      You can not simply copy the folder as it will break the things. Also this is not a good idea as the size of the folder will be large.

      We will first list all the packages installed in virtual environment and dump in a file.

      pip freeze > requirement.txt

      Now commit this file along with your code. On production server, clone the code repository and create a virtual environment. Once virtual environment is activated, install all the dependencies using the pip install command below.

      pip install -r requirement.txt

      This will create the exact copy of virtual environment you were working on development server.   


      Pipenv is a dependency manager for python projects. It is similar to Symfony's composer and node.js's npm.

      Its a higher lever tool than pip. We recommend to use pipenv for managing your virtual environments.

      With pipenv, we need not to manage requirement.txt file separately.

      As per pienv readthedocs:

      It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever–important Pipfile.lock, which is used to produce deterministic builds.

      Install pipenv using pip.

      pip install pipenv

      Creating virtual environment using pipenv:

      Go to your project directory. Install python packages using pipenv command.

      rana@Brahma: ~$ cd  /tmp
      rana@Brahma: tmp$ mkdir django_project_1
      rana@Brahma: tmp$ cd django_project_1/
      rana@Brahma: django_project_1$ pipenv install django requests
      Creating a virtualenv for this project…
      ?Using base prefix '/usr'
      New python executable in /home/rana/.local/share/virtualenvs/django_project_1-NloTdiFm/bin/python3
      Also creating executable in /home/rana/.local/share/virtualenvs/django_project_1-NloTdiFm/bin/python
      Installing setuptools, pip, wheel...done.
      Virtualenv location: /home/rana/.local/share/virtualenvs/django_project_1-NloTdiFm
      Creating a Pipfile for this project…
      Installing django…
      Collecting django
        Using cached Django-2.0-py3-none-any.whl
      Collecting pytz (from django)
        Using cached pytz-2017.3-py2.py3-none-any.whl
      Installing collected packages: pytz, django
      Successfully installed django-2.0 pytz-2017.3
      Adding django to Pipfile's [packages]…
      Installing requests…
      Collecting requests
        Using cached requests-2.18.4-py2.py3-none-any.whl
      Collecting chardet<3.1.0,>=3.0.2 (from requests)
        Using cached chardet-3.0.4-py2.py3-none-any.whl
      Collecting certifi>=2017.4.17 (from requests)
        Using cached certifi-2017.11.5-py2.py3-none-any.whl
      Collecting idna<2.7,>=2.5 (from requests)
        Using cached idna-2.6-py2.py3-none-any.whl
      Collecting urllib3<1.23,>=1.21.1 (from requests)
        Using cached urllib3-1.22-py2.py3-none-any.whl
      Installing collected packages: chardet, certifi, idna, urllib3, requests
      Successfully installed certifi-2017.11.5 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
      Adding requests to Pipfile's [packages]…
        PS: You have excellent taste! ? 

      Please subscribe to get the latest articles in your mailbox.

      Recent Posts:

      © 2018-2019