如何在 Windows 7 中运行 python 2 和 3? [英] How do I run python 2 and 3 in windows 7?

查看:30
本文介绍了如何在 Windows 7 中运行 python 2 和 3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在不破坏任何东西的情况下通过安装 python 2 安装 python 3?主要问题是我有由python xxxxx.py abc123"运行的代码.有没有办法将python 3更改为python3 xxxx.py abc123"?同样的命令python就是冲突

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

推荐答案

有一种更好的方式可以在 Windows 上共存/启动 Python 2 和 Python 3.Python 3.3 引入了 适用于 Windows 的 Python 启动器(参见 http://www.python.org/dev/peps/pep-0397/).

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

安装 Python 3.3 后,将 py.exepyw.exe 复制到您的 c:\Windows 目录,然后为 .py 扩展设置了关联,以便它使用启动器.默认情况下,Python 2 为 py script.py 启动.py -3 script.py 启动 Python 3.(这也意味着不必将 Python 的路径添加到环境中——C:\Windows 已经在PATH.)

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

最重要的是脚本中的 #!python2 导致通过 Python 2 启动,#!python3 导致通过 Python 3 启动脚本.这样,您可以在两个版本的 Python 中使用脚本,并且可以通过统一的方式启动它们——py script.py 或只需单击脚本图标.

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.

更新:当使用适用于 Windows 的 Python 启动器时,您还可以通过键入 > 从 cmd 窗口启动 Python 脚本.script.py(即无需在脚本名称前显式键入 py——Python 启动器的名称),甚至键入不带 的名称.py 扩展名(也就是 >script).

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

这样,事情开始类似于 Unix 命名脚本的方式(不需要扩展名);但是,您仍然需要在创建脚本文件时添加 .py 扩展名.

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.

(是的,它比 Unix 方法更混乱.这就是 先思考!"先销售!" 方法之间的区别操作系统的开发.无论如何,我要感谢 Python 开发团队,他们通过发布适用于 Windows 的 Python 启动器来充分利用 Windows.)

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

这篇关于如何在 Windows 7 中运行 python 2 和 3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆