我的大部分编程都是用Python 3完成的。但现在我需要使用Python成像库(PIL)、ImageMagick和wxPython,所有这些都需要Python 2.x。

我可以要两个Python 2吗?3. Python。Windows 7?当我运行一个脚本时,我如何“选择”哪个版本的Python应该运行它?上述程序是否能够处理同时安装的多个Python版本?我已经花了好几个小时来寻找如何做到这一点,但无济于事。

谢谢。


当前回答

我在windows中有多个版本。 我只是改变了我不是默认版本的exe名称。

Python.exe——> python26.exe python .exe——> pythonw26.exe

至于软件包安装程序,大多数exe安装程序也允许您选择python安装程序来添加软件包。 对于手动安装,检查——prefix选项来定义包应该安装在哪里:

http://docs.python.org/install/index.html#alternate-installation-windows-the-prefix-scheme

其他回答

在另一个上面安装最常用的一个(我的例子是3.3)。这将迫使IDLE使用您想要的那个。

或者(来自python3.3 README):

安装多个版本

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".

例如,如果你想安装Python 2.6, 2.7和3.3,其中2.7是 主版本,您将在2.7构建目录中执行“make install” 而其他的则是“make altinstall”。

我所做的就是下载2.7.6和3.3.4。Python 3.3.4提供了在环境变量中添加路径的选项,这样就完成了。所以基本上我只是手动添加了Python 2.7.6。

如何……

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.

注意:(为了不破坏步骤4和5中的pip命令,将python.exe的副本保存在与重命名文件相同的目录中)

我发现这样做的正式方式如下:

只需在Windows 7上安装两个(或两个以上,使用它们的安装程序)版本的Python(对我来说,使用3.3和2.7)。

按照下面的说明,根据需要更改参数。

创建以下环境变量(双击时为默认值):

Name:  PY_PYTHON
Value: 3

要在特定的解释器中启动脚本,添加以下shebang(脚本的开头):

#! python2

要使用特定的解释器执行脚本,请使用以下提示命令:

> py -2 MyScript.py

启动一个特定的解释器:

> py -2

要启动默认解释器(由PY_PYTHON变量定义):

> py

资源

文档:在Windows上使用Python

PEP 397 - Windows Python启动器

我刚想到一个有趣的解决办法。虽然Windows不允许您轻松地别名程序,但您可以创建重命名的批处理文件来调用当前程序。

不要重命名可执行文件,这会破坏很多东西,包括pip,而是在与python2.exe相同的目录中创建python2.bat文件。然后添加如下一行:

%~dp0python %*

这个古老的语法是什么意思?嗯,这是一个批处理脚本(bash的Windows版本)。%~dp0获取当前目录,%*将把传递给脚本的所有参数传递给python。

对python3.bat重复此步骤

您也可以为pip和其他实用程序做同样的事情,只需将文件中的python替换为pip或任何文件名。别名将是文件命名的任何内容。

最棒的是,当添加到PATH时,Windows会忽略正在运行的扩展名

python3

将启动python3版本,而命令python2将启动python2版本。

顺便说一句,这与Spyder用于将自己添加到Windows上的路径的技术相同。:)

我使用一个简单的解决方案从一个版本切换到另一个版本的python,你可以安装所有你想要的版本。你所要做的就是创造一些可变的环境。 在我的例子中,我已经安装了python 2.7和python 3.8.1,所以我创建了以下环境变量:

PYTHON_HOME_2.7 = < path_python_2.7 > PYTHON_HOME_3.8.1 = < path_python_3.8.1 > PYTHON_HOME = % PYTHON_HOME_2.7%

然后在PATH环境变量中只放入%PYTHON_HOME%和%PYTHON_HOME%\Scripts。在上面的例子中,我使用的是2.7版本,当我想切换到其他版本时,我只需要设置PYTHON_HOME=%PYTHON_HOME_3.8.1%。 我使用这种方法可以快速地从一个版本切换到另一个版本,同样适用于JAVA、MAVEN、GRADLE、ANT等等。