Multiple versions of python

C

ChrisW

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
 
D

Dave Angel

ChrisW said:
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
 
C

CCW

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?
 
D

Dave Angel

CCW said:
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 digest.py 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
digest.py 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 digest.py
is going to work on. Effectively, it raises digest.py 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
..py=Python.File

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

M:\LabOrders>


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 digest.py, if I have added the .py and .pyw extensions to my
PATHEXT environment variable. So my PATHEXT looks like:
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.PY;.PYW



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" %*
Python.File31="C:\PROGFI~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.

DaveA
 
N

Nobody

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

However: assoc and ftype modify the registry keys under
HKLM\Software\Classes. This works fine if these are the only relevant
keys, but if you also have settings under HKCU\Software\Classes, those
will take precedence, so assoc and/or ftype won't have any effect.

Also, you typically need at least "Power User" status in order to modify
the keys under HKLM, while any user should be able to modify those under
HKCU (HKCU is a "virtual" key, corresponding to HKU\<interactive user>).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top