如何在 Windows 7 中运行 python 2 和 3?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15912063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:21:33  来源:igfitidea点击:

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

pythonpython-2.7python-3.x

提问by SwimBikeRun

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 pythonis the conflict

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

采纳答案by pepr

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

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

After installation of Python 3.3, the py.exeand pyw.exeis copied to your c:\Windowsdirectory, and the associations are set for the .pyextension so that it uses the launcher. By default, Python 2 is launched for py script.py. The py -3 script.pylaunches Python 3. (This also means that no path for Python must be added to the environment -- the C:\Windowsalready is in the PATH.)

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

The best of all is that #!python2in the script causes lauching via Python 2, the #!python3causes 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.pyor by just clicking on the script icon.

最棒的是,#!python2脚本中的原因是通过 Python 2#!python3启动,原因是通过 Python 3 启动脚本。这样,您可以为 Python 的两个版本使用脚本,并且您可以以统一的方式启动它们——py script.py或者仅通过单击脚本图标。

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 cmdwindow 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 .pyextension (that is just > script).

更新:当使用适用于 Windows 的 Python 启动器时,您还可以cmd通过键入> script.py(即无需py在脚本名称前显式键入--Python 启动器的名称)或什至不通过键入名称来从窗口启动 Python 脚本在.py扩展(这只是> 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 .pyextension when creating the script file.

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

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

(是的,它比 Unix 方法更混乱。这就是操作系统开发中“先思考!”“先销售!”方法之间的区别。无论如何,我对 Python 开发团队挤压Windows 中最好的 -- 通过发布适用于 Windows 的 Python 启动器。)

回答by Shaun

Not sure if it would meet your needs, but you should take a look at virtualenv: http://www.virtualenv.org/en/latest/

不确定它是否能满足您的需求,但您应该看看 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.

这将允许您为 Python 2 和 3 创建单独的环境(使用 -p 标志)。如果您的用例对此不起作用,请使用更具体的问题更新问题,我相信您会得到其他建议。

回答by MichaelJCox

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

假设您将 python3 安装在单独的目录中,您还可以将 python 3 可执行文件重命名为 python3.exe。

回答by FA5er

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

您需要编辑环境变量以包含 Python 3 或 Python 2 路径。