我的大部分编程都是用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?

其他回答

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.

注:我使用的是Windows XP。我不知道其他版本是否会有这些变化,但我看不出有任何理由会发生变化。

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

如果您使用Anaconda Python,您可以轻松地安装各种环境。

假设你已经安装了Anaconda Python 2.7,你想要一个Python 3.4环境:

conda create -n py34 python=3.4 anaconda

然后激活环境:

activate py34

以及deactive:

deactivate py34

(对于Linux,您应该使用源代码激活py34。)

链接:

下载蟒蛇

环境说明

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

只需在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启动器

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.