views:

2056

answers:

8

I'm running windows and the shell/os automatically runs python based on the registry settings when you run a program on the command line. Will this break if I install a 2.x and 3.x version of python on the same machine?

I want to play with 3.0 while still be able to run 2.x scripts on the same machine...

+2  A: 

As far as I know Python runs off of the commandline using the PATH variable as opposed to a registry setting.

So if you point to the correct version on your PATH you will use that. Remember to restart your command prompt to use the new PATH settings.

James McMahon
This is true if you type 'python myscript.py' you could then type C:\python30\python.exe instead but the command prompt does this automatically if you just type myscript.py and I don't want to break the 2.? scripts if I install 3.0 on the same machine.
minty
Ah I see, I think Daok might have the right idea.
James McMahon
Ouch, -1, I was just trying to help.
James McMahon
Oh and minty it might help to add the information in your first comment to the question, it was alittle a little nebulous.
James McMahon
+4  A: 

You can have both installed.

You should write in front of your script :

#!/bin/env python2.6

or eventually..

#!/bin/env python3.0

Update

My solution work perfectly with Windows, after a quick search on Google, here is the Windows solution:

#!c:/Python/python3_0.exe -u

Same thing... in front of your script

Daok
This is a unix solution as far as I understand it
minty
You can run it with Cygwin...
Pokus
WEll, it can be installed on Linux, and can be on Windows too. You just need to set the env variable.. you can do that in Windows...
Daok
This solution won't work with windows (unless you call it from a unix style shell (eg cygwin)). The #! is handled by the shell, and windows does not support it. I believe in the example you googled it is being handled by the webserver, *not* from being launched in windows
Brian
Yeah, on Apache on Windows... the question request shell automaticly, you can do it with Cygwin, Apache, etc...
Daok
@Daok : I didn't downvote you. I'm grateful for help even an attempt at help.
minty
I do the same, just change the python.exe to python30.exe as mentioned.
monkut
A: 

I think there is an option to setup the windows file association for .py files in the installer. Uncheck it and you should be fine.

If not, you can easily re-associate .py files with the previous version. The simplest way is to right click on a .py file, select "open with" / "choose program". On the dialog that appears, select or browse to the version of python you want to use by default, and check the "always use this program to open this kind of file" checkbox.

Brian
This is what I did. I still have 2.5.2 and 3.0.1 on my Windows Vista box. I unchecked that in the install Wizard (might have said something like "Register with system" - I forget). Both work fine. For command line stuff, I put a py.bat file in my path that kicks off python 3 - nothing fancy, but does what I need.
Anon
+1  A: 

I would assume so, I have Python 2.4, 2.5 and 2.6 installed side-by-side on the same computer.

Dan
+2  A: 

I'm using 2.5, 2.6, and 3.0 from the shell with one line batch scripts of the form:

@c:\programs\pythonX.Y\python.exe %*

Name them pythonX.Y.bat and put them somewhere in your PATH. Copy the file for the preferred minor version (i.e. the latest) to pythonX.bat. (E.g. copy python2.6.bat python2.bat.) Then you can use python2 file.py from anywhere.

However, this doesn't help or even affect the Windows file association situation. For that you'll need a launcher program that reads the #! line, and then associate that with .py and .pyw files.

Roger Pate
A: 

You should make sure that the PATH environment variable doesn't contain both python.exe files ( add the one you're currently using to run scripts on a day to day basis ) , or do as Kniht suggested with the batch files . Aside from that , I don't see why not .

P.S : I have 2.6 installed as my "primary" python and 3.0 as my "play" python . The 2.6 is included in the PATH . Everything works fine .

Geo
A: 

The Python installation normally associates ".py", ".pyw" and ".pyc" files with the Python interpreter. So you can run a Python script either by double-clicking it in Explorer or by typing its name in a command-line window (so no need to type "python scriptname.py", just "scriptname.py" will do).

If you want to manually change this association, you can edit these keys in the Windows registry:

HKEY_CLASSES_ROOT\Python.File\shell\open\command
HKEY_CLASSES_ROOT\Python.NoConFile\shell\open\command
HKEY_CLASSES_ROOT\Python.CompiledFile\shell\open\command

But it would be great if someone would write a "Python launcher" to associate with ".py" etc files instead, which would do something like what *nix does: look for a comment on the first line something like:

#!c:\Python26\Python.exe

and then run the script using that interpreter. Or it could look for something like:

#!/usr/bin/whatever/python26

(anything of the form python**xy**), find that version installed on the PC, and run it.

Craig McQueen
+1  A: 

Here you go...

winpylaunch.py

#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
#    #!/usr/bin/python
#    #!C:\Python26\python.exe
#    # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
#    HKEY_CLASSES_ROOT\Python.File\shell\open\command
#    (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#

import subprocess
import sys

USAGE = """
USAGE: winpylaunch.py <script.py> [arg1] [arg2...]
"""

if __name__ == "__main__":
  if len(sys.argv) > 1:
    script = sys.argv[1]
    args   = sys.argv[2:]
    if script.endswith(".py"):
      interpreter = "python.exe" # Default to wherever it is found on the path.
      lines = open(script).readlines()
      for line in lines:
        if line.startswith("#!") and line.find(".exe") != -1:
          interpreter = line[2:].strip()
          break
      process = subprocess.Popen([interpreter] + [script] + args)
      process.wait()
      sys.exit()
  print(USAGE)

I've just knocked this up on reading this thread (because it's what I was needing too). I have Pythons 2.6.1 and 3.0.1 on both Ubuntu and Windows. If it doesn't work for you post fixes here.