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?

1

1 Answer 1

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 :-)

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
I was going to ask you about the sequence of numbers- what does that accomplish?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.