0

The setup: Win7 environment with Python 2.7.5 and Python 3.3.2 installed and added to the system path.

C:\\py -2

will launch Python 2.7.5,

C:\\py -3

will launch Python 3.3.2,

C:\\python

will launch Python 3.3.2.

Is it possible to toggle which Python version "python" maps to, and if so, how?

nitrl
  • 2,185
  • 2
  • 15
  • 15

1 Answers1

2

In your last line, Windows picks the first directory on your %PATH% containing a python executable. You cannot change that, short of reordering your path.

I use this little py.bat file in a directory early in my path:

\python27\python.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

So I just type py. I have a similar py3.bat to start Python 3 instead. Inside other .bat files I call py.bat or py3.bat, so they all pick up the version of Python I want when I change py.bat and/or py3.bat.

Edit: by the way, I realize my py.bat's name conflicts with the Python launcher named py. I don't care :-)

Tim Peters
  • 67,464
  • 13
  • 126
  • 132
  • That would explain it. So that I'm clear on your setup- you have created .bat files (we'll say "pythonX.bat" to avoid the conflict), and have added them to your system path, so that each of them starts the corresponding version of Python? – nitrl Oct 23 '13 at 02:18
  • Yup, that's it. I have `py.bat` (call it `py2.bat` if you like - doesn't matter) for Python 2 and `py3.bat` for Python 3. I also have many others, because I'm a Python developer, and sometimes need to access old versions (like, e.g., `py274.bat` for Python 2.7.4). BTW, on recent enough Windows versions you can use `%*` in the `.bat` files instead of the long-winded `%1 %2 ... %9`. – Tim Peters Oct 23 '13 at 02:20
  • I was going to ask you about the sequence of numbers- what does that accomplish? – nitrl Oct 23 '13 at 02:23
  • 1
    Batch files have arguments. `%1` is the first argument, `%2` the second argument, and so on. So when I run, e.g., `py -i`, the first argument is `-i`, and the batch file passes `%1 %2 ... %9` to Python so that *Python* sees the same arguments. That is `py -i` ends up running `\python27\python.exe -i`. – Tim Peters Oct 23 '13 at 02:25