I use both Python 2.7 and Python 3.3 frequently from the Windows Command Prompt. If I would only use one version, I’d add the folder containing the executable to my PATH environment variable so I did not always have to type out the full path, but only
python. However, because there are two folders with a
python.exe executable, adding both folders to the PATH wouldn’t be useful: only one of the two executables would be called when executing
python, and for the other one, I’d still have to type out the full path.
That’s why I went to look for a solution to be able to call Python 2.7 by executing the command
python2, and Python 3.3 by executing the command
python3, without renaming the executables to python2.exe and python3.exe — I wanted to keep their original file names. The solution in this post can be applied to any similar situation, I just take Python as example here.
The first step is to create a folder at any location. In this folder, create two new files:
python3.bat. These files will contain the commands to call the right Python version.
python2.bat contains this:
@echo off C:\Python27\python %*
@echo off makes sure that commands executed later are not displayed on the screen. Their output still is, but the command itself is not. If you remove that line, you’ll see this line printed when executing the file:
C:\Python27\python %* executes Python 2.7.
%* holds the arguments given to the Batch file, and these get passed to Python.
python3.bat looks very similar, only
Python27 is replaced with
Now, we have our Batch files. But we are not ready yet. Now you’d have to use the full file path to these Batch files to execute them, and the goal was to be able to call
python3 without having to enter a full path. To do this, you have to add the folder containing the Batch files to your PATH. Go to Control Panel > System and Security > System and you’ll see an “Advanced System Settings” link. Click on this one, go to the “Advanced” tab, click “Environment Variables”, select “Path”, and add your Batch folder there. Make sure it’s separated from the previous folder by a semicolon.
And now, you can call
python3 without having to write a full file path!