更改 Windows XP 上的默认 Python 解释器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4664646/
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
Change default Python interpreter on Windows XP
提问by zjm1126
I have two python versions: Python 2.5 and Python 2.7. When I'm setting up django, I want to use Python 2.7, but django is using Python 2.5 somehow.
我有两个 Python 版本:Python 2.5 和 Python 2.7。当我设置 django 时,我想使用 Python 2.7,但 django 以某种方式使用 Python 2.5。
How can I make sure that django use Python 2.7? I'm using Windows XP
如何确保 django 使用 Python 2.7?我使用的是 Windows XP
采纳答案by John Percival Hackworth
Change your PATH system environment variable to point to the version of Python you want to use.
更改 PATH 系统环境变量以指向要使用的 Python 版本。
回答by otherchirps
Changing your PATH will help, if you always call python directly, rather than relying on file association.
如果您总是直接调用 python 而不是依赖文件关联,则更改 PATH 会有所帮助。
For example: "python foo.py"will run the 'foo' script with whichever python is first on your PATH.
例如:"python foo.py"将使用 PATH 中第一个 python 运行“foo”脚本。
However, if you just run "foo.py", the handler associated in the registry, for this file extension, will be the first one called.
但是,如果您只运行"foo.py",则注册表中关联的处理程序(对于此文件扩展名)将是第一个调用的处理程序。
In your case, it sounds like that would be python 2.5. Have a look by opening regedit, and checking the values of:
在您的情况下,这听起来像是 python 2.5。通过打开 regedit 并检查以下值来查看:
HKEY_CLASSES_ROOT\Python.File\shell\open\command
The (default) value listed will probably be something like "C:\Python25\python.exe" "%1" %*
列出的(默认)值可能类似于 "C:\Python25\python.exe" "%1" %*
A quick (and dirty?) fix for you would be to change these values to the python version you actually want.
一个快速(和肮脏?)解决方法是将这些值更改为您实际想要的 Python 版本。
A better fix would be to do something like what's outlined in this feature request:
更好的解决方法是执行此功能请求中概述的操作:
http://bugs.python.org/issue4485
http://bugs.python.org/issue4485
Since then, as long as you had admin rights, you could switch as you needed by pointing assoc at the version you want quickly.
从那时起,只要您拥有管理员权限,您就可以通过将 assoc 快速指向您想要的版本来根据需要进行切换。
回答by Igor Pomaranskiy
Make two simple .cmd files:
制作两个简单的 .cmd 文件:
python25.cmd:
python25.cmd:
@echo off
set PYTHONHOME=c:\python25
set PATH=%PATH%;c:\python25
python27.cmd:
python27.cmd:
@echo off
set PYTHONHOME=c:\python27
set PATH=%PATH%;c:\python27
Now you can switch between Python 2.5 and 2.7. :)
现在您可以在 Python 2.5 和 2.7 之间切换。:)
回答by nogus
Python installer does no change to PATH environment variable under windows.
When typing something like python setup.pywindows first looks for python in PATH, then in current user registry hive
Python 安装程序不会更改 Windows 下的 PATH 环境变量。键入类似python setup.pywindows 的内容时,首先在 PATH 中查找 python,然后在当前用户注册表配置单元中查找
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths\Python.exe
and then in local machine registry hive
然后在本地机器注册表配置单元中
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\Python.exe
回答by Gerald
Now that Python 3.3 is released it is easiest to use the py.exe utility described here: http://www.python.org/dev/peps/pep-0397/
现在 Python 3.3 已发布,最容易使用此处描述的 py.exe 实用程序:http://www.python.org/dev/peps/pep-0397/
It allows you to specify a Python version in your script file using a UNIX style directive. There are also command line and environment variable options for controlling which version of Python is run.
它允许您使用 UNIX 样式指令在脚本文件中指定 Python 版本。还有用于控制运行哪个版本的 Python 的命令行和环境变量选项。
The easiest way to get this utility is to install Python 3.3 or later.
获取此实用程序的最简单方法是安装 Python 3.3 或更高版本。
回答by jnnnnn
Change the registry key at
更改注册表项在
HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command
回答by Shital Shah
If you want to switch between Python 2.x and Python 3.x then easiest way is to use Python Launcherwhich is included since 3.3 version. This is basically py.exe in Windows folder. To start Python 3.x command prompt, just type
如果你想在 Python 2.x 和 Python 3.x 之间切换,那么最简单的方法是使用Python Launcher,它从 3.3 版本开始就包含在内。这基本上是 Windows 文件夹中的 py.exe。要启动 Python 3.x 命令提示符,只需键入
py -3
To execute script with Python 3.x, use
要使用 Python 3.x 执行脚本,请使用
py -3 script.py
If you don't specify -3 then 2.x version is used by default. You can also make this explicit by using -2.7 switch.
如果不指定 -3,则默认使用 2.x 版本。您还可以通过使用 -2.7 开关使这一点明确。
py -2.7 script.py
Finally, you can now embed the version number to use in .script file itself. This works because after Python 3.3+ is installed, it associated py.exe with .py files.
最后,您现在可以嵌入版本号以在 .script 文件本身中使用。这是有效的,因为在安装 Python 3.3+ 后,它将 py.exe 与 .py 文件相关联。
#! python3
import sys
sys.stdout.write("hello from Python %s\n" % (sys.version,))

