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

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

谢谢。


当前回答

如果你不能让其他任何东西工作,打开一个解释器,无论你选择哪个版本(我更喜欢使用iPython),然后:

import subprocess

subprocess.call('python script.py -flags')

这将使用当前运行的python版本。适用于单个脚本,但如果您运行许多脚本,则很快就会失控,在这种情况下,您可以始终在其中创建一个包含所有这些调用的批处理文件。这不是最优雅的回答,但很有效。

是否有一种方法使别名不同的python版本一个la Linux?

其他回答

我刚想到一个有趣的解决办法。虽然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后检查系统环境变量,首先在PATH变量中应该是python3的目录,然后是python2。

最先匹配的路径变量就是Windows使用的路径变量。

一如既往,py -2将在此场景中启动python2。

我所做的就是下载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的副本保存在与重命名文件相同的目录中)

如果你不能让其他任何东西工作,打开一个解释器,无论你选择哪个版本(我更喜欢使用iPython),然后:

import subprocess

subprocess.call('python script.py -flags')

这将使用当前运行的python版本。适用于单个脚本,但如果您运行许多脚本,则很快就会失控,在这种情况下,您可以始终在其中创建一个包含所有这些调用的批处理文件。这不是最优雅的回答,但很有效。

是否有一种方法使别名不同的python版本一个la Linux?

要在同一系统中安装和运行任何版本的Python,请遵循下面的指南。


例如,假设你想安装Python 2。3. Python。x在同一个Windows系统上。

Install both of their binary releases anywhere you want. When prompted do not register their file extensions and do not add them automatically to the PATH environment variable Running simply the command python the executable that is first met in PATH will be chosen for launch. In other words, add the Python directories manually. The one you add first will be selected when you type python. Consecutive python programs (increasing order that their directories are placed in PATH) will be chosen like so: py -2 for the second python py -3 for the third python etc.. No matter the order of "pythons" you can: run Python 2.x scripts using the command: py -2 (Python 3.x functionality) (ie. the first Python 2.x installation program found in your PATH will be selected) run Python 3.x scripts using the command: or py -3 (ie. the first Python 3.x installation program found in your PATH will be selected)


在我的例子中,我先安装了Python 2.7.14,然后安装了Python 3.5.3。这是我的PATH变量的开头:

路径=C:\文件程序\微软MPI\ \ \;C:\Python27;C:\文件程序\ python_3.6 \ scription \;C:\文件程序\ python_3.6 \

...

请注意,Python 2.7是第一个,Python 3.5是第二个。

因此,运行python命令将启动python 2.7(如果python 3.5,同样的命令将启动python 3.5)。 运行py -2将启动Python 2.7(因为恰好第二个Python是Python 3.5,与py -2不兼容)。 运行py -3会启动Python 3.5(因为它是Python 3.x) 如果你的路径中有另一个python,你会像这样启动:py -4。如果/当Python版本4发布时,这可能会改变。

现在py -4或py -5等在我的系统输出:请求的Python版本(4)未安装或请求的Python版本(5)未安装等。

希望这已经足够清楚了。