Klik
Klik

Reputation: 1776

Is there a way to force a Python program to run in version 2.7?

Background

I have some Python scripts which use libraries only available to Python 2.7 and so I want to be able to run those in Python version 2.7.

I was thinking that it would be great if I could put some code at the top of my Python file which would detect if it was being run in Python 3 and if so execute itself in version 2.7. Is this possible?

For example, the pseudo code would be:

if (self.getPythonVersion != 2.7):
    os.execute('python27 ' + os.cwd + 'script.py')
    exit()

Edit: This is for my personal use, not for distributing.

Answer

I used mgilson's answer below to get this to work for me. I was not able to get os.exec() to work, but I didn't spend a long time on that. The second script worked for me. Here is what I used and worked for me:

if sys.version_info[:2] > (2, 7):
    code = subprocess.call(['python27', sys.argv[0] ])
    raise SystemExit(code)

Upvotes: 6

Views: 8133

Answers (4)

nabin-info
nabin-info

Reputation: 329

This ugly hack should work on most UNIX-like systems exec-magic. It relies on the triple-quote handling difference between python and sh. First sh runs the script, then reruns the script with a suitable python binary, if found.

#!/bin/sh
_HACK_='''________BEGIN_SH_CODE_____________'
ispy2() {
  case $1$2$3$4$5$6$7$8$9 in
    *ython2.*) return 0 ;;
    *) return 1 ;;
  esac
}
for c in python python2 python3 \
         /usr/local/bin/python* \
         /usr/bin/python* \
         /bin/python*
do
  ispy2 `$c -V 2>&1` && exec $c "$0" "$@"
done
echo "could not find python 2 binary"
exit 1
_HACK_='________BEGIN_PYTHON_CODE___________'''

import sys

print sys.version
print sys.argv

The nasty ispy2() function is a hack to remove whitespace from the python -V output in case of different word-splitting behaviors (I did not want to rely on any binary besides /bin/sh).

Upvotes: 0

mgilson
mgilson

Reputation: 310089

Nope, this isn't possible for the simple reason that a user could have python3.x installed and not have python2.x installed.

If you know that they have python2.7 installed, then you can use something like your work-around above, however, in that case, you'll have to make sure that you can support both python3.x and python2.x in the same source (which is generally a non-trivial task)

You can detect the python version via sys.version_info and I think you can swap out the process using something in the os.exec* family of functions... e.g.:

import os, sys

if sys.version_info[:2] > (2, 7):
    os.execve('python27', sys.argv, os.environ)

Here's another variant that you can try (it'll create a new process instead of replacing the old one however):

import sys, subprocess

if sys.version_info[:2] > (2, 7):
    code = subprocess.call(['python27'] + sys.argv)
    raise SystemExit(code)

print(sys.version_info)

Upvotes: 5

ifma
ifma

Reputation: 3818

You can try adding the python2.7 shebang line at the top of your script:

#!/usr/bin/env python2.7

Make sure it is in your path though, and this should work.

Upvotes: 5

magu_
magu_

Reputation: 4866

I would advise against this for reasons raised by mgilson. However you can check the python version with:

import sys
sys.version_info[0]

In case you still want to do this.

Upvotes: -1

Related Questions