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)