如何在 Windows 中同时安装 Python 2.x 和 Python 3.x
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3809314/
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 install both Python 2.x and Python 3.x in Windows
提问by dln385
I do most of my programming in Python 3.x on Windows 7, but now I need to use the Python Imaging Library (PIL), ImageMagick, and wxPython, all of which require Python 2.x.
我在 Windows 7 上使用 Python 3.x 进行大部分编程,但现在我需要使用 Python Imaging Library (PIL)、ImageMagick 和 wxPython,所有这些都需要 Python 2.x。
Can I have both Python 2.x and Python 3.x installed in Windows 7? When I run a script, how would I "choose" which version of Python should run it? Will the aforementioned programs be able to handle multiple versions of Python installed at once? I have searched for hours and hours for how to do this to no avail.
我可以在 Windows 7 中同时安装 Python 2.x 和 Python 3.x 吗?当我运行一个脚本时,我将如何“选择”运行它的 Python 版本?上述程序是否能够处理一次安装的多个版本的 Python?我已经搜索了数小时以了解如何执行此操作,但无济于事。
Thanks.
谢谢。
回答by li.davidm
You can install multiple versions of Python one machine, and during setup, you can choose to have one of them associate itself with Python file extensions. If you install modules, there will be different setup packages for different versions, or you can choose which version you want to target. Since they generally install themselves into the site-packages directory of the interpreter version, there shouldn't be any conflicts (but I haven't tested this). To choose which version of python, you would have to manually specify the path to the interpreter if it is not the default one. As far as I know, they would share the same PATH and PYTHONPATH variables, which may be a problem.
您可以在一台机器上安装多个 Python 版本,并且在安装过程中,您可以选择让其中之一与 Python 文件扩展名关联。如果你安装模块,不同版本会有不同的安装包,或者你可以选择你想要定位的版本。由于它们通常将自己安装到解释器版本的 site-packages 目录中,因此不应该有任何冲突(但我还没有测试过)。要选择哪个版本的 python,如果它不是默认的,你必须手动指定解释器的路径。据我所知,它们将共享相同的 PATH 和 PYTHONPATH 变量,这可能是一个问题。
Note: I run Windows XP. I have no idea if any of this changes for other versions, but I don't see any reason that it would.
注意:我运行的是 Windows XP。我不知道其他版本是否有任何更改,但我看不出有任何原因。
回答by monkut
I have multiple versions in windows. I just change the exe name of the version I'm not defaulting to.
我在 Windows 中有多个版本。我只是更改了我没有默认使用的版本的 exe 名称。
python.exe --> python26.exe
pythonw.exe --> pythonw26.exe
python.exe --> python26.exe
pythonw.exe --> pythonw26.exe
As for package installers, most exe installers allow you to choose the python install to add the package too. For manual installation check out the --prefix option to define where the package should be installed:
至于包安装程序,大多数 exe 安装程序也允许您选择 python 安装来添加包。对于手动安装,请查看 --prefix 选项以定义应安装包的位置:
http://docs.python.org/install/index.html#alternate-installation-windows-the-prefix-scheme
http://docs.python.org/install/index.html#alternate-installation-windows-the-prefix-scheme
回答by abudhabikid
Install the one you use most (3.3 in my case) over the top of the other. That'll force IDLE to use the one you want.
将您最常用的一个(在我的情况下为 3.3)安装在另一个的顶部。这将强制 IDLE 使用您想要的那个。
Alternatively (from the python3.3 README):
或者(来自 python3.3 README):
Installing multiple versions
安装多个版本
On Unix and Mac systems if you intend to install multiple versions of Python using the same installation prefix (--prefix argument to the configure script) you must take care that your primary python executable is not overwritten by the installation of a different version. All files and directories installed using "make altinstall" contain the major and minor version and can thus live side-by-side. "make install" also creates ${prefix}/bin/python3 which refers to ${prefix}/bin/pythonX.Y. If you intend to install multiple versions using the same prefix you must decide which version (if any) is your "primary" version. Install that version using "make install". Install all other versions using "make altinstall".
在 Unix 和 Mac 系统上,如果您打算使用相同的安装前缀(配置脚本的 --prefix 参数)安装多个版本的 Python,您必须注意您的主要 python 可执行文件不会被不同版本的安装覆盖。使用“make altinstall”安装的所有文件和目录都包含主要和次要版本,因此可以并排存在。“make install”还创建了 ${prefix}/bin/python3,它指的是 ${prefix}/bin/pythonX.Y。如果您打算使用相同的前缀安装多个版本,您必须决定哪个版本(如果有)是您的“主要”版本。使用“make install”安装该版本。使用“make altinstall”安装所有其他版本。
For example, if you want to install Python 2.6, 2.7 and 3.3 with 2.7 being the primary version, you would execute "make install" in your 2.7 build directory and "make altinstall" in the others.
例如,如果您想安装 Python 2.6、2.7 和 3.3,其中 2.7 是主要版本,您将在 2.7 构建目录中执行“make install”,并在其他目录中执行“make altinstall”。
回答by Pedro Vagner
I found that the formal way to do this is as follows:
我发现这样做的正式方法如下:
Just install two (or more, using their installers) versions of Python on Windows 7 (for me work with 3.3 and 2.7).
只需在 Windows 7 上安装两个(或更多,使用它们的安装程序)版本的 Python(对我来说,使用 3.3 和 2.7)。
Follow the instuctions below, changing the parameters for your needs.
按照以下说明,根据需要更改参数。
Create the following environment variable (to default on double click):
创建以下环境变量(双击时默认):
Name: PY_PYTHON
Value: 3
To launch a script in a particular interpreter, add the following shebang (beginning of script):
要在特定解释器中启动脚本,请添加以下shebang(脚本开头):
#! python2
To execute a script using a specific interpreter, use the following prompt command:
要使用特定解释器执行脚本,请使用以下提示命令:
> py -2 MyScript.py
To launch a specific interpreter:
要启动特定的解释器:
> py -2
To launch the default interpreter (defined by the PY_PYTHON variable):
启动默认解释器(由 PY_PYTHON 变量定义):
> py
Resources
资源
Documentation: Using Python on Windows
PEP 397- Python launcher for Windows
PEP 397- 适用于 Windows 的 Python 启动器
回答by Dmitry Sobolev
Starting version 3.3 Windows version has Python launcher, please take a look at section 3.4. Python Launcher for Windows
从 3.3 版开始 Windows 版有 Python 启动器,请看 3.4 节。适用于 Windows 的 Python 启动器
回答by user3295433
Only Works if your running your code in your Python IDE
仅当您在 Python IDE 中运行代码时才有效
I have both Python 2.7 and Python 3.3 installed on my windows operating system. If I try to launch a file, it will usually open up on the python 2.7 IDE. How I solved this issue, was when I choose to run my code on python 3.3, I open up python 3.3 IDLE(Python GUI), select file, open my file with the IDLE and save it. Then when I run my code, it runs to the IDLE that I currently opened it with. It works vice versa with 2.7.
我的 Windows 操作系统上安装了 Python 2.7 和 Python 3.3。如果我尝试启动一个文件,它通常会在 python 2.7 IDE 上打开。我是如何解决这个问题的,当我选择在 python 3.3 上运行我的代码时,我打开 python 3.3 IDLE(Python GUI),选择文件,用 IDLE 打开我的文件并保存它。然后当我运行我的代码时,它会运行到我当前打开它的 IDLE。它与 2.7 反之亦然。
回答by user2796022
If you can't get anything else to work, open an interpreter in whichever version you choose (I prefer using iPython) and:
如果您无法使用其他任何东西,请打开您选择的任何版本的解释器(我更喜欢使用 iPython)并且:
import subprocess
subprocess.call('python script.py -flags')
This uses whichever python version you are currently operating under. Works fine for a single script, but will quickly get out of hand if there are lots of scripts you run, in which case you can always make a batch file with all of these calls inside. Not the most elegant answer, but it works.
这将使用您当前正在运行的任何 python 版本。适用于单个脚本,但如果您运行大量脚本,很快就会失控,在这种情况下,您始终可以制作一个包含所有这些调用的批处理文件。不是最优雅的答案,但它有效。
Is there a way to make aliases for different python version a la Linux?
有没有办法在 Linux 上为不同的 python 版本制作别名?
回答by user3458330
What I did was download both 2.7.6 and 3.3.4. Python 3.3.4 has the option to add the path to it in the environment variable so that was done. So basically I just manually added Python 2.7.6.
我所做的是下载 2.7.6 和 3.3.4。Python 3.3.4 可以选择在环境变量中添加路径,这样就完成了。所以基本上我只是手动添加了 Python 2.7.6。
How to...
如何...
Start > in the search type in environment select "Edit environment variables to your account"1
Scroll down to Path, select path, click edit.
Add C:\Python27; so you should have paths to both versions of Python there, but if you don't this you can easily edit it so that you do..... C:\Python27;C:\Python33;
Navigate to the Python27 folder in C:\ and rename a copy of python.exe to python2.exe
Navigate to the Python34 folder in C:\ and rename a copy of python.exe to python3.exe
Test: open up commmand prompt and type python2 ....BOOM! Python 2.7.6. exit out.
Test: open up commmand prompt and type python3 ....BOOM! Python 3.4.3. exit out.
开始>在环境中的搜索类型中选择“将环境变量编辑到您的帐户”1
向下滚动到路径,选择路径,单击编辑。
添加 C:\Python27; 所以你应该在那里有两个版本的 Python 的路径,但如果你没有这个,你可以很容易地编辑它,这样你就可以...... C:\Python27;C:\Python33;
导航到 C:\ 中的 Python27 文件夹并将 python.exe 的副本重命名为 python2.exe
导航到 C:\ 中的 Python34 文件夹并将 python.exe 的副本重命名为 python3.exe
测试:打开命令提示符并输入 python2 ....BOOM!蟒蛇 2.7.6。退出。
测试:打开命令提示符并输入 python3 ....BOOM!蟒蛇 3.4.3。退出。
Note: (so as not to break pip commands in step 4 and 5, keep copy of python.exe in the same directory as the renamed file)
注意:(为了不破坏第4步和第5步的pip命令,python.exe的副本保存在重命名文件所在的目录下)
回答by wheya
I just had to install them. Then I used the free (and portable) soft at http://defaultprogramseditor.com/under "File type settings"/"Context menu"/search:"py", chose .py file and added an 'open' command for the 2 IDLE by copying the existant command named 'open with IDLE, changing names to IDLE 3.4.1/2.7.8, and remplacing the files numbers of their respective versions in the program path. Now I have just to right click the .py file and chose which IDLE I want to use. Can do the same with direct interpreters if you prefer.
我只需要安装它们。然后我使用了http://defaultprogramseditor.com/下“文件类型设置”/“上下文菜单”/search:“py”的免费(和便携式)软件,选择了 .py 文件并为2 IDLE 复制现有的“open with IDLE”命令,将名称改为IDLE 3.4.1/2.7.8,并在程序路径中替换各自版本的文件号。现在我只需右键单击 .py 文件并选择我想要使用的 IDLE。如果您愿意,可以对直接口译员做同样的事情。
回答by KinDa
I have encountered that problem myself and I made my launchers in a .bat so you could choose the version you want to launch.
我自己也遇到过这个问题,我用 .bat 制作了我的启动器,这样你就可以选择要启动的版本。
The only problem is your .py must be in the python folder, but anyway here is the code:
唯一的问题是你的 .py 必须在 python 文件夹中,但无论如何这里是代码:
For Python2
对于 Python2
@echo off
title Python2 Launcher by KinDa
cls
echo Type the exact version of Python you use (eg. 23, 24, 25, 26)
set/p version=
cls
echo Type the file you want to launch without .py (eg. hello world, calculator)
set/p launch=
path = %PATH%;C:\Python%version%
cd C:\Python%version%
python %launch%.py
pause
For Python3
对于 Python3
@echo off
title Python3 Launcher by KinDa
cls
echo Type the exact version of Python you use (eg. 31, 32, 33, 34)
set/p version=
cls
echo Type the file you want to launch without .py (eg. hello world, calculator)
set/p launch=
cls
set path = %PATH%:C:\Python%version%
cd C:\Python%version%
python %launch%.py
pause
Save them as .bat and follow the instructions inside.
将它们另存为 .bat 并按照其中的说明进行操作。

