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

Solution 1:

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.)

Solution 2:

Not sure if it would meet your needs, but you should take a look at virtualenv: http://www.virtualenv.org/en/latest/

This will let you create separate environments for Python 2 and 3 (using the -p flag). If your use case is something for which this wouldn't work, update the question with some more specifics and I'm sure you'll get other suggestions.

Solution 3:

Assuming that you install python3 in a separate directory, you could also rename the python 3 executable to be python3.exe.