Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 839
SwimBikeRun Avatar asked Apr 09 '13 20:04

SwimBikeRun


People also ask

Can I run python 2 and 3 on the same computer windows?

you can install both python 2 and python 3 in your machine but you can not use both in single code editor in same time. To use both at same time you have to open one IDEs with python 2 and another IDEs with python 3.

Can python 2 be run on Windows 7?

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.


3 Answers

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

like image 115
pepr Avatar answered Oct 13 '22 12:10

pepr


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.

like image 22
Shaun Avatar answered Oct 13 '22 10:10

Shaun


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

like image 2
MichaelJCox Avatar answered Oct 13 '22 10:10

MichaelJCox