1

I have two versions of Python on Windows and want to use them through cmd. I tried to make shortcuts of their python.exe and renaming them to python26 and python33 (I also added their locations to PATH), but unfortunately this does not work. Calling python26 or python26.lnk outputs in not recognized as an internal command.

Is there any other way to do it (like Linux virtualenv), or I missed something in my idea ?

PVitt
  • 11,500
  • 5
  • 51
  • 85
lagoru
  • 697
  • 1
  • 12
  • 22
  • Can you just add two batch files and use complete path in them? – longhua Oct 09 '13 at 09:30
  • Have you seen [this](http://stackoverflow.com/a/17245619/2235132)? – devnull Oct 09 '13 at 09:32
  • Rename the executables from `python.exe` to `python2.exe` and add both `C:\Python26\ ` and `C:\Python33\ ` to your **PATH** – Torxed Oct 09 '13 at 09:35
  • @Ihuang - sorry, but I quite don't understand what you are trying to say - do I have to create two batch files and add to each one complete path to each python ? – lagoru Oct 09 '13 at 09:41
  • @devnull - I want to execute specific python from cmd. I am not trying to force python files to find specific python for themselfs - but thanks for good link - it will come handy someday. – lagoru Oct 09 '13 at 09:44
  • possible duplicate of [How to run multiple python version on Windows](http://stackoverflow.com/questions/4583367/how-to-run-multiple-python-version-on-windows) – Piotr Dobrogost Oct 09 '13 at 09:46
  • @Torxed - this will work but I wanted to leave python.exe as the way it is, and operate on shortucts or some virual enviroment systems – lagoru Oct 09 '13 at 09:47
  • @lagoru How about saying `set py26=\path\to\python2.6` and invoking by saying `%py26%`? – devnull Oct 09 '13 at 09:54

4 Answers4

5

Create a new .bat file under C:\imagaginary_path\ and name it python2.bat.
Within the bat file write:

C:\Python26\python.exe %*

Then create another one under C:\imagaginary_path\ and name it python3.bat.
With the content:

C:\Python33\python.exe %*

Now remove C:\Python26\ and C:\Python33\ from your PATH and instead place C:\imaginary_path\ in your PATH variable.

There, Windows treats .bat files as executables, and now you can call python2 test.py Now every time you press Ctrl+C You will get a promt asking "Terminate batch job ?" which is kind of annoying, there's a few alternatives in order to solve this and one being you edit your python2.bat to look like:

start C:\Python26\python.exe %*
Torxed
  • 22,866
  • 14
  • 82
  • 131
  • or simply place the two `.bat` files under `C:\Windows\System32\`, works just as well. – Torxed Oct 09 '13 at 10:00
  • 1
    No! Don't put files into system32, that directory belongs to the system. – David Heffernan Oct 09 '13 at 10:41
  • @DavidHeffernan yea because windows file-structure is so sacred (although i agree, that's why i put it as a comment and not in my answer, but it would work and you wouldn't need to modify the PATH). – Torxed Oct 09 '13 at 10:45
3

As others have mentioned, creating a batch file works fine. But if you still want to use normal shortcuts (.lnk files) you can modify your PATHEXT environment variable to include .LNK. This variable tells Windows what extensions to treat as executable files when searching through the PATH variable.

For example, after creating a shortcut and adding its folder to PATH, this works:

C:\>python27
'python27' is not recognized as an internal or external command,
operable program or batch file.

C:\>echo %PATHEXT%
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

C:\>set PATHEXT=%PATHEXT%;.LNK

C:\>python27
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z


C:\>
kichik
  • 33,220
  • 7
  • 94
  • 114
0

You could also work with Windows path:

set path=C:\Python26;.;..;C:\windows;C:\windows\system32
prompt $ & start title Python26

Save this as Py26.bat and type Python in the screen that displays

set path=C:\Python33;.;..;C:\windows;C:\windows\system32
prompt $ & start title Python33

Save this as Py33.bat and type Python in the screen that displays

iChux
  • 2,266
  • 22
  • 37
0

Instead of creating shortcuts, what you could do is to change the name of python.exe itself.

So you could rename the python.exe in C:\Python26\ to 'python2', and the python.exe in C:\Python33\ to 'python3'. Given that most of the Python code right now is in < Python3, an efficient alternative would be to just change the Python 3.3 python.exe file to be 'python3', and leave the Python 2.6 one unchanged. This way, you would be able to specify if you wanted to run something using python3.

Now edit the %PATH% environment variable to include both C:\Python26\ and C:\Python33.

Example:

python3 chunky_bacon_FTW.py

would run using Python3.3.

ShivanKaul
  • 687
  • 1
  • 8
  • 21