How do I run python 2 and 3 in windows 7? [duplicate]

There is a better way of coexistence/launching of Python 2 and Python 3 on Windows. The Python 3.3 introduced the Python launcher for Windows (see http://www.python.org/dev/peps/pep-0397/).

After installation of Python 3.3, the py.exe and pyw.exe is copied to your c:\Windows directory, and the associations are set for the .py extension so that it uses the launcher. By default, Python 2 is launched for py script.py. The py -3 script.py launches Python 3. (This also means that no path for Python must be added to the environment — the C:\Windows already is in the PATH.)

The best of all is that #!python2 in the script causes lauching via Python 2, the #!python3 causes launching the script via Python 3. This way, you can use scripts for both versions of Python, and you can lauch them the unified way — py script.py or by just clicking on the script icon.

There are more details but this is basically what you need.

Update: When using Python launcher for Windows, you can also launch your Python script from cmd window by typing > script.py (that is without explicitly typing py–the name of the Python launcher–in front of the script name) or even by typing the name without the .py extension (that is just > script).

This way, things start to resemble the Unix way of naming scripts (without the need for the extension); however, you still have to add the .py extension when creating the script file.

(Yes, it is a bit more messy than the Unix approach. This is the difference between the “Think first!” and the “Sell first!” approaches of developments of the OSes. Anyway, my kudos to the Python development team to squeeze the best out of the Windows — by releasing the Python launcher for Windows.)

Leave a Comment