如何在 Windows 上运行多个 Python 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4583367/
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 multiple Python versions on Windows
提问by Bilal Basharat
I had two versions of Python installed on my machine (versions 2.6 and 2.5). I want to run 2.6 for one project and 2.5 for another.
我的机器上安装了两个版本的 Python(版本 2.6 和 2.5)。我想为一个项目运行 2.6,为另一个项目运行 2.5。
How can I specify which I want to use?
如何指定我想使用的?
I am working on Windows XP SP2.
我正在使用 Windows XP SP2。
采纳答案by aodj
Running a different copy of Python is as easy as starting the correct executable. You mention that you've started a python instance, from the command line, by simply typing python.
运行不同的 Python 副本就像启动正确的可执行文件一样简单。你提到你已经从命令行启动了一个 python 实例,只需输入python.
What this does under Windows, is to trawl the %PATH%environment variable, checking for an executable, either batch file (.bat), command file (.cmd) or some other executable to run (this is controlled by the PATHEXTenvironment variable), that matches the name given. When it finds the correct file to run the file is being run.
这在 Windows 下所做的是拖网%PATH%环境变量,检查与给定名称匹配的可执行文件,批处理文件 ( .bat)、命令文件 ( .cmd) 或其他一些要运行的可执行文件(这由PATHEXT环境变量控制)。当它找到要运行的正确文件时,该文件正在运行。
Now, if you've installed two python versions 2.5 and 2.6, the path will have both of their directories in it, something like PATH=c:\python\2.5;c:\python\2.6but Windows will stop examining the path when it finds a match.
现在,如果您安装了两个 python 版本 2.5 和 2.6,路径中将包含它们的两个目录,类似于PATH=c:\python\2.5;c:\python\2.6但 Windows 在找到匹配项时将停止检查路径。
What you really need to do is to explicitly call one or both of the applications, such as c:\python\2.5\python.exeor c:\python\2.6\python.exe.
您真正需要做的是显式调用一个或两个应用程序,例如c:\python\2.5\python.exe或c:\python\2.6\python.exe。
The other alternative is to create a shortcut to the respective python.execalling one of them python25and the other python26; you can then simply run python25on your command line.
另一种选择是创建一个快捷方式来分别python.exe调用其中一个python25和另一个python26;然后您可以简单地python25在命令行上运行。
回答by albertov
Just call the correct executable
只需调用正确的可执行文件
回答by Lennart Regebro
When you install Python, it will not overwrite other installs of other major versions. So installing Python 2.5.x will not overwrite Python 2.6.x, although installing 2.6.6 will overwrite 2.6.5.
安装 Python 时,它不会覆盖其他主要版本的其他安装。所以安装 Python 2.5.x 不会覆盖 Python 2.6.x,虽然安装 2.6.6 会覆盖 2.6.5。
So you can just install it. Then you call the Python version you want. For example:
所以你可以安装它。然后你调用你想要的 Python 版本。例如:
C:\Python2.5\Python.exe
for Python 2.5 on windows and
适用于 Windows 上的 Python 2.5 和
C:\Python2.6\Python.exe
for Python 2.6 on windows, or
适用于 Windows 上的 Python 2.6,或
/usr/local/bin/python-2.5
or
或者
/usr/local/bin/python-2.6
on WindowsUnix (including Linux and OS X).
在WindowsUnix(包括 Linux 和 OS X)上。
When you install on Unix (including Linux and OS X) you will get a generic pythoncommand installed, which will be the last one you installed. This is mostly not a problem as most scripts will explicitly call /usr/local/bin/python2.5 or something just to protect against that. But if you don't want to do that, and you probably don't you can install it like this:
当您在 Unix(包括 Linux 和 OS X)上python安装时,您将安装一个通用命令,这将是您安装的最后一个命令。这通常不是问题,因为大多数脚本会显式调用 /usr/local/bin/python2.5 或一些只是为了防止这种情况发生。但是,如果您不想这样做,并且您可能不想这样做,则可以像这样安装它:
./configure
make
sudo make altinstall
Note the "altinstall" that means it will install it, but it will notreplace the pythoncommand.
请注意“altinstall”,这意味着它将安装它,但它不会替换python命令。
On Windows you don't get a global pythoncommand as far as I know so that's not an issue.
python据我所知,在 Windows 上你没有得到全局命令,所以这不是问题。
回答by Piotr Dobrogost
Adding two more solutions to the problem:
为问题添加另外两个解决方案:
- Use pylauncher(if you have Python 3.3 or newer there's no need to install it as it comeswith Python already) and either add shebang lines to your scripts;
- 使用pylauncher(如果您有 Python 3.3 或更高版本,则无需安装它,因为它已经随 Python 一起提供了)并将 shebang 行添加到您的脚本中;
#! c:\[path to Python 2.5]\python.exe- for scripts you want to be run with Python 2.5#! c:\[path to Python 2.6]\python.exe- for scripts you want to be run with Python 2.6
#! c:\[path to Python 2.5]\python.exe- 对于您希望使用 Python 2.5 运行#! c:\[path to Python 2.6]\python.exe的脚本 - 对于您希望使用 Python 2.6 运行的脚本
or instead of running pythoncommand run pylauncher command (py) specyfing which version of Python you want;
或者代替运行python命令运行 pylauncher 命令(py)指定您想要的 Python 版本;
py -2.6– version 2.6py -2– latest installed version 2.xpy -3.4– version 3.4py -3– latest installed version 3.x
py -2.6– 版本 2.6 py -2– 最新安装的版本 2.x py -3.4– 版本 3.4 py -3– 最新安装的版本 3.x
- Install virtualenvand create two virtualenvs;
- 安装virtualenv并创建两个 virtualenv;
virtualenv -p c:\[path to Python 2.5]\python.exe [path where you want to have virtualenv using Python 2.5 created]\[name of virtualenv]
virtualenv -p c:\[path to Python 2.5]\python.exe [path where you want to have virtualenv using Python 2.5 created]\[name of virtualenv]
virtualenv -p c:\[path to Python 2.6]\python.exe [path where you want to have virtualenv using Python 2.6 created]\[name of virtualenv]
virtualenv -p c:\[path to Python 2.6]\python.exe [path where you want to have virtualenv using Python 2.6 created]\[name of virtualenv]
for example
例如
virtualenv -p c:\python2.5\python.exe c:\venvs\2.5
virtualenv -p c:\python2.5\python.exe c:\venvs\2.5
virtualenv -p c:\python2.6\python.exe c:\venvs\2.6
virtualenv -p c:\python2.6\python.exe c:\venvs\2.6
then you can activate the first and work with Python 2.5 like thisc:\venvs\2.5\activate
and when you want to switch to Python 2.6 you do
然后你可以激活第一个并像这样使用 Python 2.5 c:\venvs\2.5\activate
,当你想切换到 Python 2.6 时,你可以
deactivate
c:\venvs.6\activate
回答by Christopher Hackett
As per @alexander you can make a set of symbolic links like below. Put them somewhere which is included in your path so they can be easily invoked
根据@alexander,您可以创建一组如下所示的符号链接。将它们放在路径中包含的某个位置,以便可以轻松调用它们
> cd c:\bin
> mklink python25.exe c:\python25\python.exe
> mklink python26.exe c:\python26\python.exe
As long as c:\bin or where ever you placed them in is in your path you can now go
只要 c:\bin 或您放置它们的位置在您的路径中,您现在就可以去
> python25
回答by pepr
From Python 3.3 on, there is the official Python launcher for Windows(http://www.python.org/dev/peps/pep-0397/). Now, you can use the #!pythonXto determine the wanted version of the interpreter also on Windows. See more details in my another commentor read the PEP 397.
从 Python 3.3 开始,有适用于 Windows的官方Python 启动器( http://www.python.org/dev/peps/pep-0397/)。现在,您也可以#!pythonX在 Windows 上使用来确定所需的解释器版本。在我的另一条评论中查看更多详细信息或阅读 PEP 397。
Summary:The py script.pylaunches the Python version stated in #!or Python 2 if #!is missing. The py -3 script.pylaunches the Python 3.
摘要:如果缺少,将py script.py启动#!或 Python 2 中所述的 Python 版本#!。该py -3 script.py运行Python 3。
回答by bruceyang
cp c:\python27\bin\python.exe as python2.7.exe
cp c:\python27\bin\python.exe 作为 python2.7.exe
cp c:\python34\bin\python.exe as python3.4.exe
cp c:\python34\bin\python.exe 作为 python3.4.exe
they are all in the system path, choose the version you want to run
它们都在系统路径中,选择您要运行的版本
C:\Users\username>python2.7
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>>
C:\Users\username>python3.4
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
回答by David Greydanus
Here's a quick hack:
这是一个快速的黑客:
- Go to the directory of the version of python you want to run
- Right click on python.exe
- Select 'Create Shortcut'
- Give that shortcut a name to call by( I use p27, p33 etc.)
- Move that shortcut to your home directory(
C:\Users\Your name) - Open a command prompt and enter
name_of_your_shortcut.lnk(I usep27.lnk)
- 进入你要运行的python版本目录
- 右键单击python.exe
- 选择“创建快捷方式”
- 给该快捷方式一个名称来调用(我使用 p27、p33 等)
- 将该快捷方式移动到您的主目录(
C:\Users\Your name) - 打开命令提示符并输入
name_of_your_shortcut.lnk(我使用p27.lnk)
回答by Bogdan Oliver Stochin
Using the Rapid Environment Editoryou can push to the top the directory of the desired Python installation. For example, to start python from the c:\Python27 directory, ensure that c:\Python27 directory is before or on top of the c:\Python36 directory in the Path environment variable. From my experience, the first python executable found in the Path environment is being executed. For example, I have MSYS2 installed with Python27 and since I've added C:\MSYS2 to the path before C:\Python36, the python.exe from the C:\MSYS2.... folder is being executed.
使用快速环境编辑器,您可以将所需的 Python 安装目录推到顶部。例如,要从 c:\Python27 目录启动 python,请确保 c:\Python27 目录在 Path 环境变量中的 c:\Python36 目录之前或之上。根据我的经验,在 Path 环境中找到的第一个 python 可执行文件正在执行。例如,我使用 Python27 安装了 MSYS2,并且由于我已将 C:\MSYS2 添加到 C:\Python36 之前的路径中,因此正在执行 C:\MSYS2.... 文件夹中的 python.exe。
回答by Harsh Singh
The easiest way to run multiple versions of python on windows is described below as follows:-
在 Windows 上运行多个版本的 python 的最简单方法如下所述:-
1)Download the latest versions of pythonfrom python.org/downloadsby selecting the relevant version for your system.
1)通过选择适合您系统的相关版本,从python.org/downloads下载最新版本的python。
2)Run the installer and select Add python 3.x to the pathto set path automatically in python 3 (you just have to click the checkbox). For python 2 open up your python 2 installer, select whatever preferences you want but just remember to set Add python.exe to pathto Will be installed on local hard drive, Now just click next and wait for the installer to finish.
2)运行安装程序并选择将python 3.x添加到路径以在python 3中自动设置路径(您只需单击复选框)。对于 python 2,打开你的 python 2 安装程序,选择你想要的任何首选项,但只记得设置Add python.exe to pathto Will be installed on local hard drive,现在只需单击下一步并等待安装程序完成。
3)When both the installations are complete. Right click on my computer--Go to properties--Select advanced system settings--Go to environment variables--Click on new under System variablesand add a new system variablewith variable nameas PY_PYTHONand set this variable valueto 3. Now click on OKand you should be done.
3) 两个安装完成后。进入属性- -我的电脑上点右键选高级系统设置-进入环境变量-在系统变量单击新添加一个新的系统变量与变量名称为PY_PYTHON和这个设定变量值到3。现在单击“确定”,您应该就完成了。
4)Now to test this open the command prompt. Once you are in there type pythonor py, It should open up python3.
4)现在测试这个打开命令提示符。一旦你在那里输入python或py,它应该打开python3。
5)Now exit out of python3 by typing exit(). Now type py -2it should open python 2.
5) 现在通过输入exit() 退出python3 。现在输入py -2它应该打开 python 2。
If none of this works then restart the computer and if the problem still persists then uninstall everything and repeat the steps.
如果这些都不起作用,则重新启动计算机,如果问题仍然存在,则卸载所有内容并重复这些步骤。
Thanks.
谢谢。

