Multiple versions of python

Dave Angel davea at
Wed Jul 22 15:11:26 CEST 2009

CCW wrote:
> On 21 July, 15:19, Dave Angel <da... at> wrote:
>> ChrisW wrote:
>>> Hi,
>>> I have installed 2 versions of python on my Windows XP computer - I
>>> originally had 3.0.1, but then found that the MySQL module only
>>> supported 2.*, so I've now installed that.  I have found that if I
>>> change the Windows Environment Variable path, then I can change the
>>> version of python called when I type 'python' into a command line.
>>> However, I'd like to be able to choose which version I use.  I know
>>> that if I change C:\Python26\python.exe to
>>> C:\Python26\python2.exe and C:\Python30\python.exe to C:
>>> \Python26\python3.exe, then typing 'python2' or 'python3' will invoke
>>> the correct interpreter.  However, is it safe just to rename the
>>> executable files? Is there a more elegant way to achieve the same
>>> task?
>>> Thanks,
>>> Chris
>> The elegant way is to have a batch directory on your PATH ( I use  
>> m:\t\bat )  and put your *.bat files there.   I do NOT put any python
>> installations on the PATH.
>> For example, I have a batch file called:     m:\t\bat\python26.bat
>> c:\progfiles\python26\python.exe %*
>> The %* syntax means pass all arguments through to the program.
>> Once it all works, you can add an "@" in front of the c:   in order to
>> suppress echoing the command.  At that point, it'll look and work the
>> same way as what you did, but without modifying anything in the install
>> directory.    (You may want  python26.bat, pythonw26.bat, python31.bat
>> and pythonw31.bat)
>> The other thing you may want to do in a batch file is to change the file
>> associations so that you can run the .py file directly, without typing
>> "python" or "pythonw" in front of it.
>> The relevant Windows commands are:     assoc and ftype      And on a
>> related note, you may want to edit the PATHEXT environment variable, to
>> add .PY and .PYW
> Thanks for this - this way made a bit more sense to me.  I've now got
> C:\commands with the 4 .bat files in, and C:\commands in my path.  It
> all seems to work :) I think I've missed the point of the @ though -
> it doesn't seem to make any difference..
> I'm also a bit confused with the associations - since I've got python
> 2.6 and 3.1, surely the command I type (python26 or python31) is the
> only way to force a script to be run using a specific interpreter at
> runtime without having to change the .bat file every time I want to
> run a script using 3.1 instead of 2.6?
The @ symbol was used in older versions of CMD and COMMAND to suppress 
echoing of the command line.  I think if you're using XP or later, it 
doesn't matter.

As for file associations, I don't know just what you already know 
about.  When you type    data.doc    at a command line, the system looks 
for the data file, then if it's found, it looks up the program 
associated with that extension.  On my machine, that's a particular 
version of Word for Windows.  Similarly, if I type   and 
I'm in the directory containing that file, the system looks up my 
associations, and runs Python 2.6 on that file.  If I wanted it to run 
Python 3.1 on that file, I'd have to change the associations, temporarily.

So, first approximation, it's a way to avoid having to type PYTHON each 
time I want to run a script, as long as I don't need any other arguments 
on the (implied) command line.  Further, the system will then look for everywhere on the PATH, so I don't even have to be in the same 
directory.  And that means I can be in the data directory that 
is going to work on.  Effectively, it raises to feeling like 
an executable, for most purposes.  In fact, I put all my scripts in a 
directory, along with other simple executables.   I use  m:\t\bin  for 
that purpose.

These associations (to .py and .pyw) are established by the installer 
program for Python.  But when you had multiple installs, you could 
choose whether the second one installed overrides the first.  My point 
is that if you need to change them back and forth, you could use  assoc 
and ftype to do it.

On my system:

M:\LabOrders>assoc .py

M:\LabOrders>ftype Python.File
Python.File="C:\PROGFI~1\ACTIVE~1\python.exe" "%1" %*


So my .py files are associated with the 2.6.2 version of ActivePython 
installation.   Note the similarity of the ftype to your python26.bat file?

One more little trick is that I could just type   digest    instead 
of,   if I have added the .py and .pyw extensions to my   
PATHEXT environment variable.    So my PATHEXT looks like:

Note that the association changes made by  assoc and ftype are global.  
They apply to all cmd.exe windows, even those already running, and they 
survive a reboot.  But when you set environment variables like PATHEXT,  
you can choose to do it in a single window (with SET), or globally 
(using control-panel).

If I found myself switching often, I'd make separate ftype entries for 
each interpreter, something like
  Python.File26="C:\PROGFI~1\ACTIVE~1\python.exe" "%1" %*

and just change assoc .py  to point at the one I needed today.

As it is, I'm content leaving the associations pointing at 2.6, and 
explicitly entering PYTHON31 in front of any script name I want to run 
on that interpreter.


More information about the Python-list mailing list