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

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
SwimBikeRun
  • 4,192
  • 11
  • 49
  • 85
  • 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? – Jainathan Leung Apr 09 '13 at 20:46
  • http://j.mp/ZfKVrb maybe this will help – dnelson Apr 09 '13 at 20:46
  • 1
    No fiddling needed from Python 3.3 on. – pepr Jun 21 '13 at 23:10
  • The duplicate question is older, and the answer is different because of that fact. – pepr Sep 30 '15 at 13:34

4 Answers4

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

pepr
  • 20,112
  • 15
  • 76
  • 139
  • 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. – CMCDragonkai Nov 14 '13 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. – CMCDragonkai Nov 14 '13 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 http://www.python.org/ftp/python/2.7.6/python-2.7.6.amd64.msi (py.exe not added to C:\Windows), and then http://www.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 Nov 15 '13 at 08:46
  • Windows 8. I'll try what you did later. – CMCDragonkai Nov 15 '13 at 16:38
  • @CMCDragonkai: Well, it could be different. I did it on Windows 7. – pepr Nov 15 '13 at 19:50
  • 5
    +1 Excellent description, thanks. This question should be the one that comes up first in a Google search, unfortunately it's not. – Sabuncu Jan 18 '14 at 18:07
  • Excellent and concise. For those who want more details (in non-PEP form), some have found what I posted here to be useful: http://stackoverflow.com/a/13533958/1593924 – Jon Coombs Nov 23 '14 at 18:39
  • 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 May 08 '15 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.

Shaun
  • 2,446
  • 19
  • 33
2

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

MichaelJCox
  • 756
  • 7
  • 17
0

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

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
FA5er
  • 1
  • 3