我的大部分编程都是用Python 3完成的。但现在我需要使用Python成像库(PIL)、ImageMagick和wxPython,所有这些都需要Python 2.x。
我可以要两个Python 2吗?3. Python。Windows 7?当我运行一个脚本时,我如何“选择”哪个版本的Python应该运行它?上述程序是否能够处理同时安装的多个Python版本?我已经花了好几个小时来寻找如何做到这一点,但无济于事。
谢谢。
我的大部分编程都是用Python 3完成的。但现在我需要使用Python成像库(PIL)、ImageMagick和wxPython,所有这些都需要Python 2.x。
我可以要两个Python 2吗?3. Python。Windows 7?当我运行一个脚本时,我如何“选择”哪个版本的Python应该运行它?上述程序是否能够处理同时安装的多个Python版本?我已经花了好几个小时来寻找如何做到这一点,但无济于事。
谢谢。
当前回答
在另一个上面安装最常用的一个(我的例子是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”。
其他回答
在另一个上面安装最常用的一个(我的例子是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”。
安装Python后检查系统环境变量,首先在PATH变量中应该是python3的目录,然后是python2。
最先匹配的路径变量就是Windows使用的路径变量。
一如既往,py -2将在此场景中启动python2。
如果你不能让其他任何东西工作,打开一个解释器,无论你选择哪个版本(我更喜欢使用iPython),然后:
import subprocess
subprocess.call('python script.py -flags')
这将使用当前运行的python版本。适用于单个脚本,但如果您运行许多脚本,则很快就会失控,在这种情况下,您可以始终在其中创建一个包含所有这些调用的批处理文件。这不是最优雅的回答,但很有效。
是否有一种方法使别名不同的python版本一个la Linux?
以下是你可以做的:
安装cmd。 打开并使用cmd终端,就像你会使用cmd终端一样。 使用命令alias创建命令别名。
我做了以下事情:
alias python2 = c:\python27\python.exe
alias python3 = c:\python34\python.exe
就是这样!: -)
我刚想到一个有趣的解决办法。虽然Windows不允许您轻松地别名程序,但您可以创建重命名的批处理文件来调用当前程序。
不要重命名可执行文件,这会破坏很多东西,包括pip,而是在与python2.exe相同的目录中创建python2.bat文件。然后添加如下一行:
%~dp0python %*
这个古老的语法是什么意思?嗯,这是一个批处理脚本(bash的Windows版本)。%~dp0获取当前目录,%*将把传递给脚本的所有参数传递给python。
对python3.bat重复此步骤
您也可以为pip和其他实用程序做同样的事情,只需将文件中的python替换为pip或任何文件名。别名将是文件命名的任何内容。
最棒的是,当添加到PATH时,Windows会忽略正在运行的扩展名
python3
将启动python3版本,而命令python2将启动python2版本。
顺便说一句,这与Spyder用于将自己添加到Windows上的路径的技术相同。:)