简体   繁体   中英

Running both Python 2.7 and 3.5 on PC

I have both versions of Python installed on my PC running Windows 10 and I can switch between them manually as needed, but I was wondering if there is a way to edit their path environment variables so that I can launch both of them from the CMD easily.

For example, instead of typing "python" to launch whatever is the default one right now, I want to just type python2 for one, and python3 for the other, is that possible?

Update : it turned out that you don't need any trick for this, you just use either py -2 or py -3 accordingly. Alternatively, you can configure your own aliases in cmd as mentioned below.

This has more to do with Windows and less to do with Python IMO. You might want to take a look at Aliases in windows command prompt You should be able to use

DOSKEY python3=C:\path\to\python3.exe $*
DOSKEY python2=C:\path\to\python2.exe $*

to define the alias. You can then put those in a .cmd file eg env.cmd and use

cmd.exe /K env.cmd

to automatically load the aliases into the shell when you run it. That's the way I would go about doing this. I hope it helps.

You can try virtualenv or cygwin . Using the later you can have both versions python installed and invoked as you from the same terminal.

Another possible alternative might be Ubuntu on Windows but personally I have not tried this.

If your are looking for a native solution to use in Windows Command Prompt or Power Shell , as mentioned by Paradoxinabox you have to go with aliases.

I have copied two batch files from WinPython distribution,

cmd.bat

@echo off
call %~dp0env.bat
cmd.exe /k

and env.bat (edited)

@echo off
set WINPYDIR=C:\devel\Python34
set PATH=%WINPYDIR%\;%WINPYDIR%\DLLs;%WINPYDIR%\Scripts;%PATH%;

where WINPYDIR corresponds to the install path. I have placed these to Scripts subdirectory (for example C:\\devel\\Python34\\Scripts ), and then a suitable shortcut on desktop that launches command prompt with PATH variable set.

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