简体   繁体   中英

How to make shortcut work from PATH

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 ?

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 %*

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM