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?
-
2stackoverflow.com/questions/4583367/…samrap– samrap2013年10月23日 02:02:25 +00:00Commented Oct 23, 2013 at 2:02
1 Answer 1
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 :-)
4 Comments
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.%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.