我的大部分编程都是用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版本?我已经花了好几个小时来寻找如何做到这一点,但无济于事。
谢谢。
当前回答
如果您使用Anaconda Python,您可以轻松地安装各种环境。
假设你已经安装了Anaconda Python 2.7,你想要一个Python 3.4环境:
conda create -n py34 python=3.4 anaconda
然后激活环境:
activate py34
以及deactive:
deactivate py34
(对于Linux,您应该使用源代码激活py34。)
链接:
下载蟒蛇
环境说明
其他回答
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。我不知道其他版本是否会有这些变化,但我看不出有任何理由会发生变化。
我在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
安装Python后检查系统环境变量,首先在PATH变量中应该是python3的目录,然后是python2。
最先匹配的路径变量就是Windows使用的路径变量。
一如既往,py -2将在此场景中启动python2。
我刚想到一个有趣的解决办法。虽然Windows不允许您轻松地别名程序,但您可以创建重命名的批处理文件来调用当前程序。
不要重命名可执行文件,这会破坏很多东西,包括pip,而是在与python2.exe相同的目录中创建python2.bat文件。然后添加如下一行:
%~dp0python %*
这个古老的语法是什么意思?嗯,这是一个批处理脚本(bash的Windows版本)。%~dp0获取当前目录,%*将把传递给脚本的所有参数传递给python。
对python3.bat重复此步骤
您也可以为pip和其他实用程序做同样的事情,只需将文件中的python替换为pip或任何文件名。别名将是文件命名的任何内容。
最棒的是,当添加到PATH时,Windows会忽略正在运行的扩展名
python3
将启动python3版本,而命令python2将启动python2版本。
顺便说一句,这与Spyder用于将自己添加到Windows上的路径的技术相同。:)
Windows版本有Python启动器,请参阅3.4节。Windows Python启动器