如何在Windows中设置默认的Python版本? [英] How Should I Set Default Python Version In Windows?

查看:230
本文介绍了如何在Windows中设置默认的Python版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 7上安装了Python 2.6Python 3.1并设置了环境变量:path = d:\python2.6.

I installed Python 2.6 and Python 3.1 on Windows 7 and set environment variable: path = d:\python2.6.

当我在cmd中运行python时,它将显示python版本2.6,这是我想要的!
但是,当我在bat文件中编写脚本并运行它时,显示的python版本是3.1.

When I run python in cmd, it displays the python version 2.6, which is what I want!
But, when I wrote a script in a bat file and ran it, the displayed python version was 3.1.

    import sys
    print (sys.version)

这是怎么回事?

推荐答案

您安装的最后一个在环境中注册自身的Python是默认设置(我不记得安装程序中的确切措辞,但这是第一个选项).有许多设置,因此请确保重新安装要设置为默认版本的版本,以确保它们均被一致注册.

The last Python you install that registers itself in the environment is the default (I can't remember the exact wording in the installer, but it is the first option). There are a number of settings so to make sure they are all registered consistently just reinstall the version you want to be the default.

如果您要安装其他版本,但又不想将其作为默认版本,则只需在安装过程中禁用注册选项即可.

If you want to install another version but don't want it as the default just disable the registration option during installation.

修改(2012年5月19日)

从Python 3.3开始,Python安装程序将安装适用于Windows的Python启动器.该程序(py.exe)与Python文件扩展名关联,并寻找"shebang"注释来指定要运行的python版本.这允许多种版本的Python共存,并允许Python脚本明确指定要使用的版本.如果未指定,则默认为当前体系结构(x86或x64)使用最新的Python 2.X版本.可以通过py.ini文件或PY_PYTHON环境变量来自定义该默认值.请参见文档有关更多详细信息.

Starting with Python 3.3, the Python installer installs Python Launcher for Windows. This program (py.exe) is associated with the Python file extensions and looks for a "shebang" comment to specify the python version to run. This allows many versions of Python to co-exist and allows Python scripts to explicitly specify which version to use, if desired. If it is not specified, the default is to use the latest Python 2.X version for the current architecture (x86 or x64). This default can be customized through a py.ini file or PY_PYTHON environment variable. See the docs for more details.

只需确保Python 3.3是最后一个向Windows注册的Python.如果以后安装了其他版本的Python,请确保在环境中注册它们,因此启动器将保持默认状态.

Just make sure the Python 3.3 is the last Python installed that registered itself with Windows. If other versions of Python are installed later be sure not to register them with the environment, so the launcher will remain the default.

以下是从控制台检查启动器是否正确注册的方法:

Here's how to check if the launcher is registered correctly from the console:

C:\>assoc .py
.py=Python.File

C:\>ftype Python.File
Python.File="C:\Windows\py.exe" "%1" %*

以上,.py文件与Python.File类型关联. Python.File的命令行是Python Launcher,由于它始终位于PATH中,因此已安装在Windows目录中.

Above, .py files are associated with the Python.File type. The command line for Python.File is the Python Launcher, which is installed in the Windows directory since it is always in the PATH.

为使关联正常运行,请使用script.py从命令行运行脚本,而不要使用"python script.py",否则,将运行python而不是py. py.exe也可以与开关一起运行以强制使用Python版本:

For the association to work, run scripts from the command line with script.py, not "python script.py", otherwise python will be run instead of py. py.exe can also be run with switches to force a Python version:

py -3 script.py    # force latest Python 3.X version to be used.

另外,在PATHEXT环境变量中添加.py;.pyw;.pyc;.pyo,然后命令行可以是script,没有扩展名.

Additionally, add .py;.pyw;.pyc;.pyo to the PATHEXT environment variable and then the command line can just be script with no extension.

这篇关于如何在Windows中设置默认的Python版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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