4

I know how what to write in a python script to tell a unix box which version of python to run (#!/usr/bin/env python3.2), but how do I do that in windows. I will be deploying a program through distutils to windows boxes that have both python2.7 and 3.2 installed. I need to force it to use 2.7

Thanks!

Lance Collins
  • 3,365
  • 8
  • 41
  • 56

1 Answers1

4

Even on unix with a shebang (#!) you are not forcing which version to run under. If the program is not executed directly (./my.py) and instead is run like python2 my.py then Python 2 will still be used.

I would suggest the safest way is to check the version at the beginning of your script and bail out with an error message if it's not suitable, for example:

if sys.version_info[:3] < (3,2,0):
    print('requires Python >= 3.2.0')
    sys.exit(1)
tjm
  • 7,500
  • 2
  • 32
  • 53
  • Thanks for the version check statement. I'll be using that in all of my scripts. For this program I am distributing with distutils my expectation is that the users would just click on it to run it. How can I get it to lauch in the right version when it's run this way? – Lance Collins Nov 13 '11 at 16:15
  • 1
    @LanceCollins. No problem. I'm afraid it's been a long while since I've done anything of note on windows. There's probably a more elegant solution and hopefully someone will come up with it, but an easy one that comes straight to mind, is just ship your program with a simple `.bat` that just calls `path_to_python2 my.py`, the obvious downside being you will need to know the `path_to_python2` in advance. Have a look at http://stackoverflow.com/questions/341184/can-i-install-python-3-x-and-2-x-on-the-same-computer, it's possible some of the ideas there may help you. – tjm Nov 13 '11 at 16:47
  • 2
    Even better would be to add code to find the location of the 3.2 binary (look in the registry?) and have your script exec itself with the 3.2 binary. – Bryan Oakley Nov 13 '11 at 16:52
  • @BryanOakley. Yeah, that's a neat idea. +1 – tjm Nov 13 '11 at 16:58
  • I like the .bat file idea. I'll give it a try. When I have more time, I'll see if I can't figure out how to find the binary and run it that way. Good ideas! Thanks! – Lance Collins Nov 13 '11 at 17:54