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

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

谢谢。


当前回答

我自己也遇到过这个问题,我把我的启动器放在。bat中,这样你就可以选择你想要启动的版本。

唯一的问题是你的.py必须在python文件夹中,但不管怎样,这里是代码:

对于Python2

@echo off
title Python2 Launcher by KinDa
cls
echo Type the exact version of Python you use (eg. 23, 24, 25, 26)
set/p version=
cls
echo Type the file you want to launch without .py (eg. hello world, calculator)
set/p launch=
path = %PATH%;C:\Python%version%
cd C:\Python%version%
python %launch%.py
pause

对于Python3

@echo off
title Python3 Launcher by KinDa
cls
echo Type the exact version of Python you use (eg. 31, 32, 33, 34)
set/p version=
cls
echo Type the file you want to launch without .py (eg. hello world, calculator)
set/p launch=
cls
set path = %PATH%:C:\Python%version%
cd C:\Python%version%
python %launch%.py
pause

将它们保存为。bat,并按照里面的说明进行操作。

其他回答

我按照这里的说明分三步做到了这一点:这都是从这里直接获取的: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之间进行选择。

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.

我在自己安装了Python 2.7和Python 3.4的windows计算机上所做的是,我在与Python.exe文件相同的目录下编写了一个简单的。bat文件。它们看起来像,

cmd /k "c:\python27\python.exe" %*

%*允许您随后添加参数(Python文件)。我相信/k在运行完脚本后会保持提示打开。然后我将其保存为python27.bat,然后我进入Python 3目录并在那里创建一个bat文件。现在我可以在命令行中写了

Python27 helloworld.py

Or

Python34 helloworld.py

它们将在各自版本的Python中运行。确保c:\python27和c:\python34在你的环境变量中。

我从这里得到了答案

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

要在同一系统中安装和运行任何版本的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)未安装等。

希望这已经足够清楚了。