问题:我可以在同一台Windows计算机上安装Python 3.x和2.x吗?

我正在运行Windows,并且在命令行上运行程序时,shell / OS将根据注册表设置自动运行Python。如果我在同一台计算机上安装2.x和3.x版本的Python,这会中断吗?

我想玩Python 3,同时仍然能够在同一台机器上运行2.x脚本。

I’m running Windows and the shell/OS automatically runs Python based on the registry settings when you run a program on the command line. Will this break if I install a 2.x and 3.x version of Python on the same machine?

I want to play with Python 3 while still being able to run 2.x scripts on the same machine.


回答 0

共存的官方解决方案似乎是WindowsPython Launcher,PEP 397,它包含在Python 3.3.0中。将发行版转储文件py.exepyw.exe启动器安装到%SYSTEMROOT%C:\Windows)中,然后分别与pypyw脚本关联。

为了使用新启动器(无需手动设置自己的关联),请保持“注册扩展名”选项处于启用状态。我不太确定为什么,但是在我的机器上,它保留了Py 2.7作为(启动器的)“默认”值。

通过直接从命令行调用脚本来运行脚本,这些脚本将通过启动程序进行路由并解析shebang(如果存在)。您还可以显式调用启动器并使用开关:py -3 mypy2script.py

各种各样的shebangs似乎都可以工作

  • #!C:\Python33\python.exe
  • #!python3
  • #!/usr/bin/env python3

以及肆意滥用

  • #! notepad.exe

The official solution for coexistence seems to be the Python Launcher for Windows, PEP 397 which was included in Python 3.3.0. Installing the release dumps py.exe and pyw.exe launchers into %SYSTEMROOT% (C:\Windows) which is then associated with py and pyw scripts, respectively.

In order to use the new launcher (without manually setting up your own associations to it), leave the “Register Extensions” option enabled. I’m not quite sure why, but on my machine it left Py 2.7 as the “default” (of the launcher).

Running scripts by calling them directly from the command line will route them through the launcher and parse the shebang (if it exists). You can also explicitly call the launcher and use switches: py -3 mypy2script.py.

All manner of shebangs seem to work

  • #!C:\Python33\python.exe
  • #!python3
  • #!/usr/bin/env python3

as well as wanton abuses

  • #! notepad.exe

回答 1

这是我的设置:

  1. 使用Windows安装程序安装Python 2.7和3.4 。
  2. 转到C:\Python34(默认安装路径)并将python.exe更改为python3.exe
  3. 编辑 环境变量以包括C:\Python27\;C:\Python27\Scripts\;C:\Python34\;C:\Python34\Scripts\;

现在在命令行中,您可以使用python2.7和python33.4。

Here’s my setup:

  1. Install both Python 2.7 and 3.4 with the windows installers.
  2. Go to C:\Python34 (the default install path) and change python.exe to python3.exe
  3. Edit your environment variables to include C:\Python27\;C:\Python27\Scripts\;C:\Python34\;C:\Python34\Scripts\;

Now in command line you can use python for 2.7 and python3 for 3.4.


回答 2

您可以同时安装。

您应该在脚本之前编写以下代码:

#!/bin/env python2.7

或者,最终…

#!/bin/env python3.6

更新资料

Google上进行快速搜索后,我的解决方案与Unix完美搭配,这是Windows解决方案:

#!c:/Python/python3_6.exe -u

同样的事情:在脚本之前。

You can have both installed.

You should write this in front of your script:

#!/bin/env python2.7

or, eventually…

#!/bin/env python3.6

Update

My solution works perfectly with Unix, after a quick search on Google, here is the Windows solution:

#!c:/Python/python3_6.exe -u

Same thing: in front of your script.


回答 3

从3.3版开始,Python引入了适用于Windows的Launcher实用程序https://docs.python.org/3/using/windows.html#python-launcher-for-windows

为了能够使用多个版本的Python:

  1. 安装Python 2.x(x是您需要的任何版本)
  2. 安装Python 3.x(x是您需要的任何版本,您还必须拥有一个3.x> = 3.3的版本)
  3. 打开命令提示符
  4. 键入py -2.x以启动Python 2.x
  5. 键入py -3.x启动Python 3.x

From version 3.3 Python introduced Launcher for Windows utility https://docs.python.org/3/using/windows.html#python-launcher-for-windows.

So to be able to use multiple versions of Python:

  1. install Python 2.x (x is any version you need)
  2. install Python 3.x (x is any version you need also you have to have one version 3.x >= 3.3)
  3. open Command Prompt
  4. type py -2.x to launch Python 2.x
  5. type py -3.x to launch Python 3.x

回答 4

我从外壳程序中使用2.5、2.6和3.0,以及以下形式的一行批处理脚本:

:: The @ symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
@c:\programs\pythonX.Y\python.exe %*

命名它们pythonX.Y.bat并将它们放在PATH中的某个位置。将首选的次要版本(即最新版本)的文件复制到pythonX.bat。(例如copy python2.6.bat python2.bat。)然后您可以python2 file.py在任何地方使用。

但是,这没有帮助甚至影响Windows文件关联的情况。为此,您需要一个启动器程序来读取该#!行,然后将其与.py和.pyw文件关联。

I’m using 2.5, 2.6, and 3.0 from the shell with one line batch scripts of the form:

:: The @ symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
@c:\programs\pythonX.Y\python.exe %*

Name them pythonX.Y.bat and put them somewhere in your PATH. Copy the file for the preferred minor version (i.e. the latest) to pythonX.bat. (E.g. copy python2.6.bat python2.bat.) Then you can use python2 file.py from anywhere.

However, this doesn’t help or even affect the Windows file association situation. For that you’ll need a launcher program that reads the #! line, and then associate that with .py and .pyw files.


回答 5

将两者都添加到环境变量时,将发生冲突,因为两个可执行文件具有相同的名称:python.exe

只需重命名其中之一即可。就我而言,我将其重命名为python3.exe

因此,当我运行python它将执行python.exe2.7,而当我运行python3将执行python3.exe3.6

在此处输入图片说明

When you add both to environment variables there will a be a conflict because the two executable have the same name: python.exe.

Just rename one of them. In my case I renamed it to python3.exe.

So when I run python it will execute python.exe which is 2.7 and when I run python3 it will execute python3.exe which is 3.6

enter image description here


回答 6

干得好…

winpylaunch.py

#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
#    #!/usr/bin/python
#    #!C:\Python26\python.exe
#    # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
#    HKEY_CLASSES_ROOT\Python.File\shell\open\command
#    (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#

import subprocess
import sys

USAGE = """
USAGE: winpylaunch.py <script.py> [arg1] [arg2...]
"""

if __name__ == "__main__":
  if len(sys.argv) > 1:
    script = sys.argv[1]
    args   = sys.argv[2:]
    if script.endswith(".py"):
      interpreter = "python.exe" # Default to wherever it is found on the path.
      lines = open(script).readlines()
      for line in lines:
        if line.startswith("#!") and line.find(".exe") != -1:
          interpreter = line[2:].strip()
          break
      process = subprocess.Popen([interpreter] + [script] + args)
      process.wait()
      sys.exit()
  print(USAGE)

我刚刚在阅读此线程时就搞定了(因为这也是我所需要的)。我在Ubuntu和Windows上都有Python 2.6.1和3.0.1。如果对您不起作用,请在此处发布修复程序。

Here you go…

winpylaunch.py

#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
#    #!/usr/bin/python
#    #!C:\Python26\python.exe
#    # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
#    HKEY_CLASSES_ROOT\Python.File\shell\open\command
#    (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#

import subprocess
import sys

USAGE = """
USAGE: winpylaunch.py <script.py> [arg1] [arg2...]
"""

if __name__ == "__main__":
  if len(sys.argv) > 1:
    script = sys.argv[1]
    args   = sys.argv[2:]
    if script.endswith(".py"):
      interpreter = "python.exe" # Default to wherever it is found on the path.
      lines = open(script).readlines()
      for line in lines:
        if line.startswith("#!") and line.find(".exe") != -1:
          interpreter = line[2:].strip()
          break
      process = subprocess.Popen([interpreter] + [script] + args)
      process.wait()
      sys.exit()
  print(USAGE)

I’ve just knocked this up on reading this thread (because it’s what I was needing too). I have Pythons 2.6.1 and 3.0.1 on both Ubuntu and Windows. If it doesn’t work for you post fixes here.


回答 7

据我所知,Python使用PATH变量而不是注册表设置来从命令行运行。

因此,如果您在PATH上指向正确的版本,则将使用该版本。请记住,重新启动命令提示符以使用新的PATH设置。

As far as I know Python runs off of the commandline using the PATH variable as opposed to a registry setting.

So if you point to the correct version on your PATH you will use that. Remember to restart your command prompt to use the new PATH settings.


回答 8

Python安装通常会将.py.pyw.pyc文件与Python解释器相关联。因此,您可以通过在资源管理器中双击Python脚本或在命令行窗口中键入其名称来运行Python脚本(这样就无需键入python scriptname.py,就scriptname.py可以了)。

如果要手动更改此关联,则可以在Windows注册表中编辑以下项:

HKEY_CLASSES_ROOT\Python.File\shell\open\command
HKEY_CLASSES_ROOT\Python.NoConFile\shell\open\command
HKEY_CLASSES_ROOT\Python.CompiledFile\shell\open\command

Python启动器

人们一直在研究Windows的Python启动器:与.py.pyw文件相关联的轻量级程序,它将在第一行中寻找“ shebang”行(类似于Linux等),并以2.x或3.x版本启动Python。需要。有关详细信息,请参见“适用于Windows的Python启动器”博客文章。

The Python installation normally associates .py, .pyw and .pyc files with the Python interpreter. So you can run a Python script either by double-clicking it in Explorer or by typing its name in a command-line window (so no need to type python scriptname.py, just scriptname.py will do).

If you want to manually change this association, you can edit these keys in the Windows registry:

HKEY_CLASSES_ROOT\Python.File\shell\open\command
HKEY_CLASSES_ROOT\Python.NoConFile\shell\open\command
HKEY_CLASSES_ROOT\Python.CompiledFile\shell\open\command

Python Launcher

People have been working on a Python launcher for Windows: a lightweight program associated with .py and .pyw files which would look for a “shebang” line (similar to Linux et al) on the first line, and launch Python 2.x or 3.x as required. See “A Python Launcher for Windows” blog post for details.


回答 9

尝试使用Anaconda。

假设使用Anaconda环境的概念,您需要Python 3来学习编程,但是您不想通过更新Python来消灭Python 2.7环境。您可以创建并激活名为“ snakes”(或所需的任何名称)的新环境,并按如下所示安装最新版本的Python 3:

conda create --name snakes python=3

它比听起来简单,请在此处查看介绍页面:Anaconda入门

然后要解决并排运行2.x和3.x版本的特定问题,请参阅:使用Anaconda管理Python版本

Try using Anaconda.

Using the concept of Anaconda environments, let’s say you need Python 3 to learn programming, but you don’t want to wipe out your Python 2.7 environment by updating Python. You can create and activate a new environment named “snakes” (or whatever you want), and install the latest version of Python 3 as follows:

conda create --name snakes python=3

Its simpler than it sounds, take a look at the intro page here: Getting Started with Anaconda

And then to handle your specific problem of having version 2.x and 3.x running side by side, see:


回答 10

这是在同一台机器上运行Python 2和3的方法

  1. 安装Python 2.x
  2. 安装Python 3.x
  3. 启动Powershell
  4. 输入Python -2以启动Python 2.x
  5. 输入Python -3启动Python 2.x

WindowsPython启动器自3.3版开始嵌入到Python中,正如2011年Stand Stand首次亮相时所承诺的那样:

适用于Windows的Python启动器

Here is how to run Python 2 and 3 on the same machine

  1. install Python 2.x
  2. install Python 3.x
  3. Start Powershell
  4. Type Python -2 to launch Python 2.x
  5. Type Python -3 to launch Python 2.x

The Python Launcher for Windows was embedded into Python since Version 3.3, as promised in 2011 when the Stand alone first made its debut:

Python Launcher for Windows


回答 11

这是在Windows上安装Python2和Python3的一种简洁方法。

https://datascience.com.co/how-to-install-python-2-7-and-3-6-in-windows-10-add-python-path-281e7eae62a

我的情况:我必须安装Apache cassandra。我已经在D:驱动器中安装了Python3 。随着大量开发工作的进行,我不想弄乱我的Python3安装。而且,我只需要Python2用于Apache cassandra。

所以我采取了以下步骤:

  1. 下载并安装了Python2。
  2. 已将Python2条目添加到类路径(C:\Python27;C:\Python27\Scripts
  3. python.exe修改为python2.exe(如下图所示)

在此处输入图片说明

  1. 现在我可以同时运行两者。适用于Python 2(python2 --version)和Python 3(python --version)。 在此处输入图片说明

因此,我的Python3安装保持不变。

Here is a neat and clean way to install Python2 & Python3 on windows.

https://datascience.com.co/how-to-install-python-2-7-and-3-6-in-windows-10-add-python-path-281e7eae62a

My case: I had to install Apache cassandra. I already had Python3 installed in my D: drive. With loads of development work under process i didn’t wanted to mess my Python3 installation. And, i needed Python2 only for Apache cassandra.

So i took following steps:

  1. Downloaded & Installed Python2.
  2. Added Python2 entries to classpath (C:\Python27;C:\Python27\Scripts)
  3. Modified python.exe to python2.exe (as shown in image below)

enter image description here

  1. Now i am able to run both. For Python 2(python2 --version) & Python 3 (python --version). enter image description here

So, my Python3 installation remained intact.


回答 12

我认为可以在安装程序中为.py文件设置Windows文件关联。取消选中它就可以了。

如果没有,您可以轻松地将.py文件与以前的版本重新关联。最简单的方法是右键单击.py文件,选择“打开方式” /“选择程序”。在出现的对话框中,选择或浏览到默认情况下要使用的python版本,然后选中“始终使用此程序打开这种文件”复选框。

I think there is an option to setup the windows file association for .py files in the installer. Uncheck it and you should be fine.

If not, you can easily re-associate .py files with the previous version. The simplest way is to right click on a .py file, select “open with” / “choose program”. On the dialog that appears, select or browse to the version of python you want to use by default, and check the “always use this program to open this kind of file” checkbox.


回答 13

您应该确保PATH环境变量不包含两个python.exe文件(添加您当前用于每天运行脚本的文件),或按照批处理文件的建议进行操作。除此之外,我不明白为什么不这样。

PS:我安装了2.6作为“主要” python,安装了3.0作为“ play” python。2.6包含在PATH中。一切正常。

You should make sure that the PATH environment variable doesn’t contain both python.exe files ( add the one you’re currently using to run scripts on a day to day basis ) , or do as Kniht suggested with the batch files . Aside from that , I don’t see why not .

P.S : I have 2.6 installed as my “primary” python and 3.0 as my “play” python . The 2.6 is included in the PATH . Everything works fine .


回答 14

在我勇敢地同时安装两者之前,我有很多问题。如果我给python,我要py2时会转到py3吗?pip / virtualenv是否会在py2 / 3下发生?

现在看来非常简单。

只需盲目安装它们。确保您获得正确的类型(x64 / x32)。在安装时/安装后,请确保添加到环境变量的路径。

[ENVIRONMENT]::SETENVIRONMENTVARIABLE("PATH", "$ENV:PATH;C:\PYTHONx", "USER")

替换上面命令中的x以设置路径。

然后转到两个文件夹。

导航

python3.6/Scripts/

并将pip重命名为pip3。

如果pip3已经存在,请删除该pip。这将确保just pip将在python2下运行。您可以通过以下方式进行验证:

pip --version

如果您想在python3中使用pip,请使用

pip3 install 

您可以类似地对python文件和其他文件执行相同的操作。

干杯!

Before I courageously installed both simultaneously, I had so many questions. If I give python will it go to py3 when i want py2? pip/virtualenv will happen under py2/3?

It seems to be very simple now.

Just blindly install both of them. Make sure you get the right type(x64/x32). While/after installing make sure you add to the path to your environment variables.

[ENVIRONMENT]::SETENVIRONMENTVARIABLE("PATH", "$ENV:PATH;C:\PYTHONx", "USER")

Replace the x in the command above to set the path.

Then go to both the folders.

Navigate to

python3.6/Scripts/

and rename pip to pip3.

If pip3 already exists delete the pip. This will make sure that just pip will run under python2. You can verify by:

pip --version

In case you want to use pip with python3 then just use

pip3 install 

You can similarly do the same to python file and others.

Cheers!


回答 15

Easy-peasy,在安装了两个python版本之后,将路径添加到环境变量;请参阅环境变量设置。然后转到python 2和python 3文件夹,分别将它们重命名为python2和python3,如图在这里python2和所示在这里python3。现在在cmd中键入python2或python3以使用所需的版本,请参见这里

Easy-peasy ,after installing both the python versions add the paths to the environment variables ;seeenvironment variable settings. Then go to python 2 and python 3 folders and rename them to python2 and python3 respectively as shown here for python2 and here for python3. Now in cmd type python2 or python3 to use your required version see here.


回答 16

我假设是这样,我在同一台计算机上并排安装了Python 2.4、2.5和2.6。

I would assume so, I have Python 2.4, 2.5 and 2.6 installed side-by-side on the same computer.


回答 17

我现在刚开始使用python。我正在阅读Zed Shaw的书“以困难的方式学习Python”,该书需要python 2.x版本,但同时也要学习一个需要python 3.x的类。

这就是我所做的。

  1. 下载python 2.7
  2. 运行电源外壳(应该已经在Windows上安装)
  3. 在POWERSHELL中运行python(如果无法识别,请转到步骤4)
  4. 仅当powershell无法识别python 2.7时,才输入以下内容:

“ [[环境] :: SETENVIRONMENTVARIABLE(“ PATH”,“ $ ENV:PATH; C:\ PYTHON27”,“ USER”)“(无外部引号)

  1. 现在键入python,您应该看到它说python 2.7等等等等

现在适用于python 3.x

简单,适用于Windows应用程序的python 3.x下载带有python。因此,只需将适用于Windows的Python应用程序固定到任务栏,或创建桌面快捷方式即可完成!

打开适用于Windows的3.x版Python

打开适用于python 2.x的Powershell

我希望这有帮助!

I am just starting out with python now. I’m reading Zed Shaw’s book “Learn Python the Hard Way” which requires python version 2.x but am also taking a class that requires python 3.x

So here is what I did.

  1. Download python 2.7
  2. run power shell (should already be installed on windows)
  3. run python IN POWERSHELL (if it doesn’t recognize then go to step 4)
  4. Only if powershell doesn’t recognize python 2.7 type in the following:

“[ENVIRONMENT]::SETENVIRONMENTVARIABLE(“PATH”, “$ENV:PATH;C:\PYTHON27”, “USER”)” (no outside quotes)

  1. Now type python and you should see it say python 2.7 blah blah blah

NOW for python 3.x

Simple, python 3.x download comes with python for windows app. SO simply pin the Python for Windows app to your task bar, or create shortcut to the desktop and you are done!

Open Python for Windows for 3.x

Open Powershell for python 2.x

I hope this helps!


回答 18

嗯..我现在通过在https://www.python.org/downloads/release/python-365/下载适用于Windows的Python 3.6.5来做到这一点,并确保将安装启动器。然后,我按照使用python 2和python 3的说明进行操作。重新启动命令提示符,然后使用py -2.7来使用Python 2和pypy -3.6来使用Python3。您还可以将其pip2用于Python 2 pippipPython 3 pip

Hmm..I did this right now by just downloading Python 3.6.5 for Windows at https://www.python.org/downloads/release/python-365/ and made sure that the launcher would be installed. Then, I followed the instructions for using python 2 and python 3. Restart the command prompt and then use py -2.7 to use Python 2 and py or py -3.6 to use Python 3. You can also use pip2 for Python 2’s pip and pip for Python 3’s pip.


回答 19

我在要使用python3进行大多数工作时遇到了同样的问题,但是IDA pro需要python2。所以,这就是我所做的。

我首先在用户环境变量中创建了3个变量,如下所示:

  1. PYTHON_ACTIVE:最初为空
  2. HOME_PYTHON27:具有安装Python 2的文件夹的路径。例如。“; /脚本;”
  3. HOME_PYTHON38:类似于python 2,此变量包含python 3文件夹的路径。

现在我加了

%PYTHON_ACTIVE%

到PATH变量。因此,基本上说这个“ PYTHON_ACTIVE”包含的内容就是活动的python。我们以编程方式更改“ PYTHON_ACTIVE”的包含内容以切换python版本。

这是示例脚本:

:: This batch file is used to switch between python 2 and 3.
@ECHO OFF

set /p choice= "Please enter '27' for python 2.7 , '38' for python 3.8 : "

IF %choice%==27 (
setx PYTHON_ACTIVE %HOME_PYTHON27%
)

IF %choice%==38 (
setx PYTHON_ACTIVE %HOME_PYTHON38%
)


PAUSE

该脚本将python版本作为输入,并因此将HOME_PYTHON27或HOME_PYTHON38复制到PYTHON_ACTIVE。从而更改了全局Python版本。

I had the same problem where I wanted to use python3 for most work but IDA pro required python2. SO, here’s what I did.

I first created 3 variables in the user environment variable as follows:

  1. PYTHON_ACTIVE : This is initially empty
  2. HOME_PYTHON27 : Has a path to a folder where Python 2 is installed. Eg. “;/scripts;”
  3. HOME_PYTHON38 : Similar to python 2, this variable contains a path to python 3 folders.

Now I added

%PYTHON_ACTIVE%

to PATH variable. So, basically saying that whatever this “PYTHON_ACTIVE” contains is the active python. We programmatically change the contains of “PYTHON_ACTIVE” to switch python version.

Here is the example script:

:: This batch file is used to switch between python 2 and 3.
@ECHO OFF

set /p choice= "Please enter '27' for python 2.7 , '38' for python 3.8 : "

IF %choice%==27 (
setx PYTHON_ACTIVE %HOME_PYTHON27%
)

IF %choice%==38 (
setx PYTHON_ACTIVE %HOME_PYTHON38%
)


PAUSE

This script takes python version as input and accordingly copies HOME_PYTHON27 or HOME_PYTHON38 to PYTHON_ACTIVE. Thus changing the global Python version.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。