我的大部分编程都是用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版本?我已经花了好几个小时来寻找如何做到这一点,但无济于事。
谢谢。
当前回答
要在同一系统中安装和运行任何版本的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)未安装等。
希望这已经足够清楚了。
其他回答
我发现这样做的正式方式如下:
只需在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启动器
我按照这里的说明分三步做到了这一点:这都是从这里直接获取的:http://ipython.readthedocs.io/en/stable/install/kernel_install.html。我目前正在运行Python 2。已安装Anaconda 4.2.13。
1)首先安装最新版本的python:
conda create -n python3 python=3 ipykernel
2)下一步激活python3
activate python3
3)安装内核:
python -m ipykernel install --user
如果你已经安装了python3,想要安装python2,请切换上面的python2和python3。当你打开一个新的笔记本时,你现在可以在python2或python3之间进行选择。
在另一个上面安装最常用的一个(我的例子是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。
只有在Python IDE中运行代码才有效
我的windows操作系统上同时安装了Python 2.7和Python 3.3。如果我试图启动一个文件,它通常会在python 2.7 IDE上打开。我如何解决这个问题,是当我选择在python 3.3上运行我的代码时,我打开python 3.3 IDLE(python GUI),选择文件,用IDLE打开我的文件并保存它。然后,当我运行我的代码时,它运行到我当前打开它的IDLE。反之亦然。