如何在cmd中运行不同的python版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20786478/
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
How to run different python versions in cmd
提问by hamidfzm
How can I configure windows command dialog to run different python versions in it? For example when I type python2it runs python 2.7 and when I type python3it runs python 3.3? I know how to configure environment variables for one version but two? I mean something like Linux terminal.
如何配置 Windows 命令对话框以在其中运行不同的 python 版本?例如,当我输入python2它时运行 python 2.7,当我输入python3它时运行 python 3.3?我知道如何为一个版本配置环境变量,但如何为两个版本配置环境变量?我的意思是类似于 Linux 终端。
采纳答案by pepr
Python 3.3 introduces Python Launcher for Windows that is installed into c:\Windows\as py.exeand pyw.exeby the installer. The installer also creates associations with .pyand .pyw. Then add #!python3or #!python2as the first lline. No need to add anything to the PATHenvironment variable.
Python的3.3主要介绍Python启动的Windows是安装到c:\Windows\作为py.exe和pyw.exe安装程序。安装程序还创建与.py和 的关联.pyw。然后添加#!python3或#!python2作为第一行。无需向PATH环境变量添加任何内容。
Update:Just install Python 3.3 from the official python.org/download. It will add also the launcher. Then add the first line to your script that has the .pyextension. Then you can launch the script by simply typing the scriptname.pyon the cmd line, od more explicitly by py scriptname.py, and also by double clicking on the scipt icon.
更新:只需从官方python.org/download安装 Python 3.3 。它还将添加启动器。然后将第一行添加到具有.py扩展名的脚本中。然后,您可以通过scriptname.py在 cmd 行上简单地键入od来启动脚本,通过od 更明确地键入py scriptname.py,也可以双击 scipt 图标。
The py.exelooks for C:\PythonXX\python.exewhere XXis related to the installed versions of Python at the computer. Say, you have Python 2.7.6 installed into C:\Python27, and Python 3.3.3 installed into C:\Python33. The first line in the script will be used by the Python launcher to choose one of the installed versions. The default (i.e. without telling the version explicitly) is to use the highest version of Python 2 that is available on the computer.
在py.exe外观精美C:\PythonXX\python.exe,其中XX涉及到Python的在电脑中安装的版本。假设您已将 Python 2.7.6 安装到 中C:\Python27,并将 Python 3.3.3 安装到C:\Python33. Python 启动器将使用脚本中的第一行来选择一个已安装的版本。默认(即没有明确告知版本)是使用计算机上可用的最高版本的 Python 2。
回答by martineau
I would suggest using the Python Launcher for Windowsutility that was introduced into Python 3.3. You can manually download and install it directly from the author's websitefor use with earlier versions of Python 2 and 3.
我建议使用Python 3.3 中引入的Python Launcher for Windows实用程序。您可以直接从作者的网站手动下载并安装它,以便与 Python 2 和 3 的早期版本一起使用。
Regardless of how you obtain it, after installation it will have associated itself with all the standard Python file extensions (i.e. .py,.pyw, .pyc, and .pyofiles). You'll not only be able to explicitly control which version is used at the command-prompt, but also on a script-by-script basis by adding Linux/Unix-y shebang #!/usr/bin/env pythonXcomments at the beginning of your Python scripts.
不管你如何获得它,安装后它会与所有标准的Python文件扩展名(即赞同.py,.pyw,.pyc和.pyo文件)。您不仅可以在命令提示符下明确控制使用哪个版本,还可以通过#!/usr/bin/env pythonX在 Python 脚本的开头添加 Linux/Unix-y shebang注释来逐个脚本地控制。
回答by Leung Ying Ying
I also met the case to use both python2 and python3 on my Windows machine. Here's how i resolved it:
我也遇到过在我的 Windows 机器上同时使用 python2 和 python3 的情况。这是我解决它的方法:
- download python2x and python3x, installed them.
- add
C:\Python35;C:\Python35\Scripts;C:\Python27;C:\Python27\Scriptsto environment variablePATH. - Go to
C:\Python35to renamepython.exetopython3.exe, also toC:\Python27, renamepython.exetopython2.exe. - restart your command window.
- type
python2 scriptname.py, orpython3 scriptname.pyin command line to switch the version you like.
- 下载python2x和python3x,安装它们。
- 添加
C:\Python35;C:\Python35\Scripts;C:\Python27;C:\Python27\Scripts到环境变量PATH。 - 转到
C:\Python35重命名python.exe为python3.exe,也C:\Python27重命名python.exe为python2.exe。 - 重新启动命令窗口。
- 键入
python2 scriptname.py, 或python3 scriptname.py在命令行中切换您喜欢的版本。

