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

2019-01-06 09:19发布

This question already has an answer here:

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条回答
Deceive 欺骗
2楼-- · 2019-01-06 09:41

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

查看更多
萌系小妹纸
3楼-- · 2019-01-06 09:54

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.

查看更多
太酷不给撩
4楼-- · 2019-01-06 09:58

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

查看更多
▲ chillily
5楼-- · 2019-01-06 10:02

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

查看更多
登录 后发表回答