6

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.

HUSMEN
  • 184
  • 2
  • 13
  • You could just create copies of both `python.exe`s and name them `python2.exe` and `python3.exe`. – Aran-Fey Jul 22 '16 at 17:47
  • A simple solution would be to go to the directories where they're installed and copy `python.exe` to `pythonX.exe`, where X is whatever suffix you want, and then put both directories in your PATH. – Ross Ridge Jul 22 '16 at 17:47
  • I considered that already but I thought that this method might cause other problems somewhere else, after a second thought I guess it won't. Thank you! – HUSMEN Jul 22 '16 at 17:59
  • 3
    Can't you use `py -2` and `py -3`? – Valentin Lorentz Jul 22 '16 at 19:22
  • I wasn't even aware that this exists! Thank you @ValentinLorentz – HUSMEN Jul 22 '16 at 23:23

3 Answers3

2

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 e.g. 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.

Community
  • 1
  • 1
  • This is what I needed exactly, Thanks. – HUSMEN Jul 22 '16 at 23:10
  • Windows console aliases are very limited. They're not real commands. You can't use them in scripts or batch files. You can't even pipe to them. They're just a simple text replacement matched at the beginning of a line of input that's read from the console (conhost.exe). One useful thing about them is that you can load a different set of aliases for each program that's attached to the console (e.g. cmd.exe, powershell.exe, cdb.exe, or python.exe). This is most conveniently accomplished using the `/macrofile` option of [doskey.exe](https://technet.microsoft.com/en-us/library/cc753867). – Eryk Sun Jul 24 '16 at 01:21
  • For an actual command, other than using py.exe, you can use `mklink` to create a hard or symbolic link to python.exe. You could also create a .lnk shell shortcut to python.exe. Clear its "start in" field to inherit the working directory of the command prompt, and add .LNK to the `PATHEXT` environment variable to avoid having to type the extension. – Eryk Sun Jul 24 '16 at 01:27
0

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.

Community
  • 1
  • 1
forevergenin
  • 1,180
  • 12
  • 14
0

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.

J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61