我的大部分编程都是用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版本?我已经花了好几个小时来寻找如何做到这一点,但无济于事。
谢谢。
当前回答
我自己也遇到过这个问题,我把我的启动器放在。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,并按照里面的说明进行操作。
其他回答
我所做的就是下载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的副本保存在与重命名文件相同的目录中)
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 10pro上安装了python 2.7.13和python 3.6.1,当我尝试pip2或pip3时,我得到了相同的“致命错误”。
为了纠正这一点,我去python2和python3文件的python.exe的位置,并为每个文件创建一个副本,然后根据安装文件夹中的python版本将每个副本重命名为python2.exe和python3.exe。因此,我在每个python安装文件夹中都有一个python.exe文件和一个python2.exe或python3.exe,这取决于python版本。
当我输入pip2或pip3时,这解决了我的问题。
我发现这样做的正式方式如下:
只需在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启动器
我使用一个简单的解决方案从一个版本切换到另一个版本的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等等。