I have a RPi 3 running Raspbian Stretch.

My plan is to write a Python program to control a 4-relay module to switch lights on and off at predetermined times.

I’ve used tkinter to create a UI to select the on/off times which works fine.

I had hoped to use Python-crontab to create/edit a crontab within python, based on my UI selections which would call another python script to switch the lights on/off.

I installed python-crontab using:

pip install python-crontab

Which seemed to have been successful.

My Python script starts:


from crontab import CronTab

Which generates the ‘ImportError: No module named 'crontab'’

python-crontab seems just perfect for what I’m trying to do. Can anyone help please?

  • 1
    Perhaps it is Python2? From the command line type python and at the prompt try import crontab.
    – joan
    Nov 25, 2017 at 16:13
  • This sounds promising! pi@raspberrypi:~ $ python Python 2.7.13 (default, Jan 19 2017, 14:48:08) [GCC 6.3.0 20170124] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import crontab >>> So no error! I'm using Thonny as my editor. Is there a way to force it to use Python 2.7.13? I've tried adding it to #!/usr/bin/ to no avail. Thanks very much for your help
    – DickFromMK
    Nov 25, 2017 at 16:31
  • Perhaps there is a Python3 version of the module (Python2 and Python3 are sufficiently different to need different modules). The Debian convention is to use python3-xxx for Python3 modules. Perhaps try pip install python3-crontab.
    – joan
    Nov 25, 2017 at 16:46
  • Fabulous help Joan! Thank-you! Using Python 2 (IDLE) python-crontab all seems to be working OK. importing Tkinter and my UI all seems to work too! I tried your suggestion for python3-crontab, but unfortunately it failed with 'No matching distribution found for python3-crontab' but hopefully I'll be able to do what I need in python 2. Again, thanks very much for your help!
    – DickFromMK
    Nov 25, 2017 at 16:56

1 Answer 1


python-crontab supports Python 3, but does need to be installed for Python 3. As noted in the Pi docs:

pip3 installs modules for Python 3, and pip installs modules for Python 2.

If you would like to install python-crontab for Python 3, you must run:

sudo pip3 install python-crontab

It also appears to be available in the Debian repositories for Python 3 (install with sudo apt-get install python3-crontab). It is a little outdated though at version 1.9.3 for Stretch, so if possible, pip is the preferable solution, which will install version 2.2.7.

Alternatively, if you wish for your script to be run using Python 2, you can change your shebang to read:


If you are able to use Python 3, though, it's generally advisable to use it — Python 2 doesn't receive the latest updates, and will, eventually, reach its end of life in 2020. Python 3 does have far fewer quirks, so I strongly advise you to consider using the methods above and install for Python 3 instead.

  • 1
    Excellent! I used sudo pip3 install python-crontab which has worked perfectly! That's a day I won't see again! Thanks again, it's much appreciated.
    – DickFromMK
    Nov 25, 2017 at 17:05
  • Actually, what has got it working in Thonny was using sudo apt-get install python3-crontab. Thanks again.
    – DickFromMK
    Nov 25, 2017 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.