标签归档:build-environment

如何在Windows中同时安装Python 2.x和Python 3.x

问题:如何在Windows中同时安装Python 2.x和Python 3.x

我在Windows 7上使用Python 3.x进行大部分编程,但是现在我需要使用Python Imaging Library(PIL),ImageMagick和wxPython,所有这些都需要Python2.x。

我可以在Windows 7中同时安装Python 2.x和Python 3.x吗?运行脚本时,如何“选择”应该运行哪个版本的Python?前面提到的程序能否处理一次安装的多个版本的Python?我已经搜索了数小时,但无济于事。

谢谢。

I do most of my programming in Python 3.x on Windows 7, but now I need to use the Python Imaging Library (PIL), ImageMagick, and wxPython, all of which require Python 2.x.

Can I have both Python 2.x and Python 3.x installed in Windows 7? When I run a script, how would I “choose” which version of Python should run it? Will the aforementioned programs be able to handle multiple versions of Python installed at once? I have searched for hours and hours for how to do this to no avail.

Thanks.


回答 0

我发现执行此操作的正式方法如下:

只需在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启动器

I found that the formal way to do this is as follows:

Just install two (or more, using their installers) versions of Python on Windows 7 (for me work with 3.3 and 2.7).

Follow the instuctions below, changing the parameters for your needs.

Create the following environment variable (to default on double click):

Name:  PY_PYTHON
Value: 3

To launch a script in a particular interpreter, add the following shebang (beginning of script):

#! python2

To execute a script using a specific interpreter, use the following prompt command:

> py -2 MyScript.py

To launch a specific interpreter:

> py -2

To launch the default interpreter (defined by the PY_PYTHON variable):

> py

Resources

Documentation: Using Python on Windows

PEP 397 – Python launcher for Windows


回答 1

我所做的是下载了2.7.6和3.3.4。Python 3.3.4可以选择在环境变量中添加路径。所以基本上我只是手动添加了Python 2.7.6。

如何…

  1. 开始>在环境中的搜索类型中,选择“将环境变量编辑到您的帐户” 1

  2. 向下滚动到路径,选择路径,然后单击编辑。

  3. 添加C:\ Python27; 因此,您应该在那里拥有两个版本的Python的路径,但是如果没有,则可以轻松地对其进行编辑,以便您执行….. C:\ Python27; C:\ Python33;

  4. 导航到C:\中的Python27文件夹,并将python.exe的副本重命名为python2.exe

  5. 导航到C:\中的Python34文件夹,并将python.exe的副本重命名为python3.exe

  6. 测试:打开命令提示符并输入python2 …. BOOM!Python 2.7.6。退出。

  7. 测试:打开命令提示符并输入python3 …. BOOM!Python 3.4.3。退出。

注意:(为了避免在第4步和第5步中破坏pip命令,请将python.exe的副本保留在与重命名文件相同的目录中)

What I did was download both 2.7.6 and 3.3.4. Python 3.3.4 has the option to add the path to it in the environment variable so that was done. So basically I just manually added Python 2.7.6.

How to…

  1. Start > in the search type in environment select “Edit environment variables to your account”1

  2. Scroll down to Path, select path, click edit.

  3. 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;

  4. Navigate to the Python27 folder in C:\ and rename a copy of python.exe to python2.exe

  5. Navigate to the Python34 folder in C:\ and rename a copy of python.exe to python3.exe

  6. Test: open up commmand prompt and type python2 ….BOOM! Python 2.7.6. exit out.

  7. Test: open up commmand prompt and type python3 ….BOOM! Python 3.4.3. exit out.

Note: (so as not to break pip commands in step 4 and 5, keep copy of python.exe in the same directory as the renamed file)


回答 2

我在Windows中有多个版本。我只是更改了非默认版本的exe名称。

python.exe-> python26.exe

pythonw.exe-> pythonw26.exe

至于软件包安装程序,大多数exe安装程序都允许您选择python安装来添加软件包。对于手动安装,请检查–prefix选项以定义软件包的安装位置:

http://docs.python.org/install/index.html#alternate-installation-windows-the-prefix-scheme

I have multiple versions in windows. I just change the exe name of the version I’m not defaulting to.

python.exe –> python26.exe

pythonw.exe –> pythonw26.exe

As for package installers, most exe installers allow you to choose the python install to add the package too. For manual installation check out the –prefix option to define where the package should be installed:

http://docs.python.org/install/index.html#alternate-installation-windows-the-prefix-scheme


回答 3

如果使用Anaconda Python,则可以轻松安装各种环境。

假设您已安装Anaconda Python 2.7,并且想要一个Python 3.4环境:

conda create -n py34 python=3.4 anaconda

然后激活环境:

activate py34

并停用:

deactivate py34

(对于Linux,您应该使用 source activate py34。)

链接:

下载Anaconda Python

环境说明

If you use Anaconda Python, you can easily install various environments.

Say you had Anaconda Python 2.7 installed and you wanted a python 3.4 environment:

conda create -n py34 python=3.4 anaconda

Then to activate the environment:

activate py34

And to deactive:

deactivate py34

(With Linux, you should use source activate py34.)

Links:

Download Anaconda Python

Instructions for environments


回答 4

要在同一系统中安装和运行任何版本的Python,请遵循以下指南。


例如,说您想在同一Windows系统上安装Python 2.x和Python3.x。

  1. 在需要的任何位置安装它们的两个二进制发行版。

    • 出现提示时,请勿注册其文件扩展名和
    • 不要将它们自动添加到PATH环境变量中
  2. 仅运行命令python,便会选择要在PATH中首次遇到的可执行文件进行启动。换句话说,手动添加Python目录。键入时,将选择第一个添加的对象python。像这样选择连续的python程序(将其目录放置在PATH中的顺序增加):

    • py -2第二 python
    • py -3表示第三个python等等。
  3. 无论“ pythons”的顺序如何,您都可以:

    • 使用以下命令运行Python 2.x脚本:py -2(Python 3.x功能)(即,将选择在PATH中找到的第一个Python 2.x安装程序)
    • 使用以下命令运行Python 3.x脚本:或py -3(即,将选择在PATH中找到的第一个Python 3.x安装程序)

在我的示例中,我先安装了Python 2.7.14,然后安装了Python 3.5.3。这是我的PATH变量以以下内容开头的方式:

PATH = C:\ Program Files \ Microsoft MPI \ Bin \; C:\ Python27; C:\ Program Files \ Python_3.6 \ Scripts \; C:\ Program Files \ Python_3.6 \; C:\ ProgramData \ Oracle \ Java \ javapath; C:\ Program Files(x86)\ Common Files \ Intel \ Shared

请注意,Python 2.7是第一位,Python 3.5是第二位。

  • 因此,运行python命令将启动python 2.7(如果使用Python 3.5,则同一命令将启动Python 3.5)。
  • Running 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 -4py -5等在我的系统上输出:Requested Python version (4) not installedRequested Python version (5) not installed等。

希望这已经足够清楚了。

To install and run any version of Python in the same system follow my guide below.


For example say you want to install Python 2.x and Python 3.x on the same Windows system.

  1. 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
  2. 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..
  3. 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)

In my example I have Python 2.7.14 installed first and Python 3.5.3. This is how my PATH variable starts with:

PATH=C:\Program Files\Microsoft MPI\Bin\;C:\Python27;C:\Program Files\Python_3.6\Scripts\;C:\Program Files\Python_3.6\;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Intel\Shared

Note that Python 2.7 is first and Python 3.5 second.

  • So running python command will launch python 2.7 (if Python 3.5 the same command would launch Python 3.5).
  • Running py -2 launches Python 2.7 (because it happens that the second Python is Python 3.5 which is incompatible with py -2). Running py -3 launches Python 3.5 (because it’s Python 3.x)
  • If you had another python later in your path you would launch like so: py -4. This may change if/when Python version 4 is released.

Now py -4 or py -5 etc. on my system outputs: Requested Python version (4) not installed or Requested Python version (5) not installed etc.

Hopefully this is clear enough.


回答 5

从3.3版开始,Windows版具有Python启动器,请查看3.4节。适用于Windows的Python启动器

Starting version 3.3 Windows version has Python launcher, please take a look at section 3.4. Python Launcher for Windows


回答 6

您可以执行以下操作:

安装cmder。像使用cmd终端一样打开并使用Cmder。使用命令别名来创建命令别名。

我做了以下事情:

alias python2 = c:\python27\python.exe
alias python3 = c:\python34\python.exe

就是这样!;-)

Here’s what you can do:

Install cmder. Open and use Cmder as you would with you cmd terminal. Use the command alias to create command aliases.

I did the following:

alias python2 = c:\python27\python.exe
alias python3 = c:\python34\python.exe

And that’s it! ;-)


回答 7

我实际上只是想到了一个有趣的解决方案。尽管Windows不允许您轻松给程序加上别名,但是您可以创建重命名的批处理文件来调用当前程序。

不要重命名可执行文件,否则将破坏很多东西,包括pip,请在与python2.exe相同的目录中创建文件python2.bat。然后添加以下行:

%~dp0python %*

这个古老的语法是什么意思?好吧,这是一个批处理脚本(Windows版本的bash)。%〜dp0获取当前目录,%*会将所有传递给脚本的参数传递给python。

对python3.bat重复

您还可以对pip和其他实用程序执行相同的操作,只需将文件中的python单词替换为pip或任何文件名即可。别名将是文件的名称。

最好的是,Windows添加到PATH时会忽略扩展名,因此运行

python3

将启动python3版本,命令python2将启动python2版本。

顺便说一句,这与Spyder用于将自身添加到Windows上的路径的技术相同。:)

I actually just thought of an interesting solution. While Windows will not allow you to easily alias programs, you can instead create renamed batch files that will call the current program.

Instead of renaming the executable which will break a lot of thing including pip, create the file python2.bat in the same directory as the python2.exe. Then add the following line:

%~dp0python %*

What does this archaic syntax mean? Well, it’s a batch script, (Windows version of bash). %~dp0 gets the current directory and %* will just pass all the arguments to python that were passed to the script.

Repeat for python3.bat

You can also do the same for pip and other utilities, just replace the word python in the file with pip or whathever the filename. The alias will be whatever the file is named.

Best of all, when added to the PATH, Windows ignores the extension so running

python3

Will launch the python3 version and and the command python2 will launch the python2 version.

BTW, this is the same technique Spyder uses to add itself to the path on Windows. :)


回答 8

您可以在一台计算机上安装多个版本的Python,并且在安装过程中可以选择让其中一个与Python文件扩展名关联。如果安装模块,则将有不同版本的安装程序包,或者您可以选择要定位的版本。由于他们通常会将自己安装到解释器版本的site-packages目录中,因此不应有任何冲突(但我尚未对此进行测试)。要选择哪个版本的python,您必须手动指定解释器的路径(如果它不是默认路径)。据我所知,它们将共享相同的PATH和PYTHONPATH变量,这可能是一个问题。

注意:我运行Windows XP。我不知道其他版本是否会发生任何更改,但我看不出会有任何原因。

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.

Note: I run Windows XP. I have no idea if any of this changes for other versions, but I don’t see any reason that it would.


回答 9

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

要么

Python34 helloworld.py

它们将在各自的Python版本中运行。确保C:\ python27C:\ python34在您的环境变量。

我从这里得到答案

What I have done on my own windows computer where I have Python 2.7 and Python 3.4 installed is I wrote a simple .bat file in the same directory as my Python.exe files. They look something like,

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

The %* allows you to add arguments (Python files) afterwards. I believe /k keeps the prompt open after it finishes running the script. Then I save that as python27.bat Then I go to my Python 3 directory and make a bat file there. Now in my command line I can write

Python27 helloworld.py

Or

Python34 helloworld.py

And they will run in their respective versions of Python. Make sure that c:\python27 and c:\python34 are in your environment variables.

I got my answer from here


回答 10

我按照此处的说明分三个步骤进行了操作:全部直接从此处获取:http : //ipython.readthedocs.io/en/stable/install/kernel_install.html。我目前在Windows 8上运行Python 2.x,并安装了Anaconda 4.2.13。

1)首先安装最新版本的python:

conda create -n python3 python=3 ipykernel

2)接下来激活python3

activate python3

3)安装内核:

python -m ipykernel install --user

如果您已经安装了Python 3并且想要安装2,请切换上面的2和3。当您打开一个新笔记本时,现在可以在Python 2或3之间进行选择。

I did this in three steps by following the instructions here: This is all taken directly from here: http://ipython.readthedocs.io/en/stable/install/kernel_install.html. I’m currently running Python 2.x on Windows 8 and have Anaconda 4.2.13 installed.

1) First install the latest version of python:

conda create -n python3 python=3 ipykernel

2) Next activate python3

activate python3

3) Install the kernel:

python -m ipykernel install --user

If you have Python 3 installed and want to install 2, switch the 2 and the 3 above. When you open a new notebook, you can now choose between Python 2 or 3.


回答 11

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

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

和往常一样,在这种情况下py -2将启动python2。

Check your system environment variables after installing Python, python 3’s directories should be first in your PATH variable, then python 2.

Whichever path variable matches first is the one Windows uses.

As always py -2 will launch python2 in this scenario.


回答 12

我自己遇到了这个问题,我在.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并按照其中的说明进行操作。

I have encountered that problem myself and I made my launchers in a .bat so you could choose the version you want to launch.

The only problem is your .py must be in the python folder, but anyway here is the code:

For 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

For 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

Save them as .bat and follow the instructions inside.


回答 13

将最常用的一个(在本例中为3.3)安装在另一个的顶部。这将迫使IDLE使用您想要的那个。

或者(来自python3.3自述文件):

安装多个版本

在Unix和Mac系统上,如果要使用相同的安装前缀(配置脚本的–prefix参数)安装多个版本的Python,则必须注意,安装其他版本不会覆盖您的主要python可执行文件。使用“ make altinstall”安装的所有文件和目录都包含主要版本和次要版本,因此可以并行存在。“ make install”还会创建$ {prefix} / bin / python3,它引用了$ {prefix} /bin/pythonX.Y。如果打算使用相同的前缀安装多个版本,则必须确定哪个版本(如果有)是您的“主要”版本。使用“进行安装”安装该版本。使用“ make altinstall”安装所有其他版本。

例如,如果要安装Python 2.6、2.7和3.3,而2.7是主要版本,则可以在2.7构建目录中执行“ make install”,在其他构建目录中执行“ make altinstall”。

Install the one you use most (3.3 in my case) over the top of the other. That’ll force IDLE to use the one you want.

Alternatively (from the python3.3 README):

Installing multiple versions

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”.

For example, if you want to install Python 2.6, 2.7 and 3.3 with 2.7 being the primary version, you would execute “make install” in your 2.7 build directory and “make altinstall” in the others.


回答 14

我只需要安装它们。然后,我在http://defaultprogramseditor.com/的 “文件类型设置” /“上下文菜单” /搜索:“ py”下使用了免费的(可移植的)软件,选择了.py文件,并为该文件添加了“打开”命令2 IDLE,方法是复制名为“用IDLE打开”的现有命令,将名称更改为IDLE 3.4.1 / 2.7.8,并在程序路径中重新复制各个版本的文件号。现在,我只需右键单击.py文件,然后选择我要使用的IDLE。如果愿意,可以使用直接口译员做同样的事情。

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.


回答 15

仅当您在Python IDE中运行代码时才有效

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

Only Works if your running your code in your Python IDE

I have both Python 2.7 and Python 3.3 installed on my windows operating system. If I try to launch a file, it will usually open up on the python 2.7 IDE. How I solved this issue, was when I choose to run my code on python 3.3, I open up python 3.3 IDLE(Python GUI), select file, open my file with the IDLE and save it. Then when I run my code, it runs to the IDLE that I currently opened it with. It works vice versa with 2.7.


回答 16

我在Windows 10pro上同时安装了python 2.7.13和python 3.6.1,当我尝试使用pip2或pip3时,出现了同样的“致命错误”。

我所做的纠正操作是转到python.exe的python 2和python 3文件的位置,并为每个文件创建一个副本,然后根据其中的python版本,将每个副本重命名为python2.exe和python3.exe。安装文件夹。因此,我在每个python安装文件夹中都有python.exe文件和python2.exe或python3.exe(取决于python版本)。

当我键入pip2或pip3时,这解决了我的问题。

I have installed both python 2.7.13 and python 3.6.1 on windows 10pro and I was getting the same “Fatal error” when I tried pip2 or pip3.

What I did to correct this was to go to the location of python.exe for python 2 and python 3 files and create a copy of each, I then renamed each copy to python2.exe and python3.exe depending on the python version in the installation folder. I therefore had in each python installation folder both a python.exe file and a python2.exe or python3.exe depending on the python version.

This resolved my problem when I typed either pip2 or pip3.


回答 17

如果您无法使用其他任何功能,请打开所选版本的解释器(我更喜欢使用iPython),然后执行以下操作:

import subprocess

subprocess.call('python script.py -flags')

这将使用您当前正在使用的python版本。对于单个脚本来说效果很好,但是如果您运行的脚本很多,则会很快失去控制,在这种情况下,您始终可以在其中包含所有这些调用的情况下创建批处理文件。不是最优雅的答案,但它可以工作。

有没有一种方法可以为Linux中的不同python版本创建别名?

If you can’t get anything else to work, open an interpreter in whichever version you choose (I prefer using iPython) and:

import subprocess

subprocess.call('python script.py -flags')

This uses whichever python version you are currently operating under. Works fine for a single script, but will quickly get out of hand if there are lots of scripts you run, in which case you can always make a batch file with all of these calls inside. Not the most elegant answer, but it works.

Is there a way to make aliases for different python version a la Linux?