7

crontab is using version 2.6 to run a script that requires 2.7 to run. How do I set the default version of Python to be 2.7 permanently? running ./file.py works fine, its just when its run through crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
*,30 *  *  *  * root /root/file.py >>/tmp/log.txt 2>&1

edit issue resolved

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin/python
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
*,30 *  *  *  * root /usr/local/bin/python2.7 /root/file.py >>/tmp/log.txt 2>&1
8
  • does your enviornment has python 2.7 installed ? Commented Jun 27, 2015 at 19:37
  • yes I can run ./file.py fine just when its run through crontab its an issue Commented Jun 27, 2015 at 19:40
  • where is python2.7 installed? Commented Jun 27, 2015 at 19:41
  • ok in your shell type which python and use the path and add it as the first path in your PATH variable Commented Jun 27, 2015 at 19:41
  • 1
    Try which python and which python2.7 on the command line. Commented Jun 27, 2015 at 19:56

2 Answers 2

7

If you have a separate version of python installed for 2.7 you can look for it with whereis python

I have CentOS 6 which comes with 2.6 by default, so this command returns:

python2: /usr/bin/python2.6 /usr/bin/python2.6-config /usr/bin/python2 /usr/lib/python2.6 /usr/lib64/python2.6 /usr/local/bin/python2.7-config /usr/local/bin/python2.7 /usr/local/lib/python2.7 /usr/include/python2.6

Of these /usr/local/bin/python2.7 is the one I'm interested in, so when I put a job in crontab, I choose it explicitly:

30 00 * * * /usr/local/bin/python2.7 /home/mike/job.py
Sign up to request clarification or add additional context in comments.

Comments

4

Update the shebang of your Python script to use Python 2.7 explicitly:

#!/usr/bin/env python2.7

This first line tells Unix which interpreter to use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.