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

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

谢谢。


当前回答

我使用一个简单的解决方案从一个版本切换到另一个版本的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等等。

其他回答

安装Python后检查系统环境变量,首先在PATH变量中应该是python3的目录,然后是python2。

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

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

只有在Python IDE中运行代码才有效

我的windows操作系统上同时安装了Python 2.7和Python 3.3。如果我试图启动一个文件,它通常会在python 2.7 IDE上打开。我如何解决这个问题,是当我选择在python 3.3上运行我的代码时,我打开python 3.3 IDLE(python GUI),选择文件,用IDLE打开我的文件并保存它。然后,当我运行我的代码时,它运行到我当前打开它的IDLE。反之亦然。

我在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

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.

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。我不知道其他版本是否会有这些变化,但我看不出有任何理由会发生变化。