44

Is there a way to install python 3 over an installation of python 2 without ruining anything? The main issue is that I have code that runs by "python xxxxx.py abc123". Is there a way to change python 3 to be "python3 xxxx.py abc123"? The same command python is the conflict

4
  • It's certainly possible, as "installing python" is just extracting a bunch of folders. Just run the installers and you'll have a python26 and python32 folders in your C drive. Concerning the command, you'll need some fiddling with the environment variables every time you want to use the other, so maybe a short bash script to modify the PATH is needed? Commented Apr 9, 2013 at 20:46
  • j.mp/ZfKVrb maybe this will help
    – dnelson
    Commented Apr 9, 2013 at 20:46
  • 1
    No fiddling needed from Python 3.3 on.
    – pepr
    Commented Jun 21, 2013 at 23:10
  • The duplicate question is older, and the answer is different because of that fact.
    – pepr
    Commented Sep 30, 2015 at 13:34

4 Answers 4

95

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

8
  • 8
    I don't see any py.exe or pyw.exe. But when I launch Python I get: Fatal Python error: Py_Initialize: unable to load the file system codec File "C:\Python27\lib\encodings\__init__.py", line 123 raise CodecRegistryError,\ ^ SyntaxError: invalid syntax. Which means python3 is trying to launch python2 stuff. Commented Nov 14, 2013 at 19:34
  • Actually the py.exe is in the C:\Python33. But if I try launching py -3, I get the same error as above. Commented Nov 14, 2013 at 19:36
  • @CMCDragonkai: I cannot confirm that. I have just uninstalled all Python versions I had on my disk, physically removing also the C:\PythonXX (with site packages, etc.), physically removed also C:\Windows\py.exe and pyw.exe. Then I have downloaded and installed python.org/ftp/python/2.7.6/python-2.7.6.amd64.msi (py.exe not added to C:\Windows), and then python.org/ftp/python/3.3.2/python-3.3.2.amd64.msi -- py.exe and pyw.exe added to C:\Windows. What version of Windows do you have?
    – pepr
    Commented Nov 15, 2013 at 8:46
  • 5
    +1 Excellent description, thanks. This question should be the one that comes up first in a Google search, unfortunately it's not.
    – Sabuncu
    Commented Jan 18, 2014 at 18:07
  • 5
    Starting with a fresh copy of Windows 8.1, I installed Python 2.7.9 and then Python 3.4.3 (both with default options). Python 3 did indeed add py.exe and pyw.exe to C:\Windows, and everything works as described by @pepr. Thanks!
    – Pakman
    Commented May 8, 2015 at 21:47
6

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.

0
2

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

1
  • 2
    Renaming the .exe breaks the use of py -3, which was suggested by @pepr.
    – Kevin
    Commented Jan 19, 2015 at 20:17
0

You need to edit your environment variable to include your Python 3 or Python 2 path.

Not the answer you're looking for? Browse other questions tagged or ask your own question.