代码之家  ›  专栏  ›  技术社区  ›  minty

我可以在同一台windows计算机上安装python 3.x和2.x吗?

  •  131
  • minty  · 技术社区  · 16 年前

    我运行的是windows,当您在命令行上运行程序时,shell/os会根据注册表设置自动运行python。如果我在同一台机器上安装2.x和3.x版本的python,这会中断吗?

    我想玩Python3,同时还能在同一台机器上运行2.x脚本。

    17 回复  |  直到 6 年前
        1
  •  63
  •   Nick T twasbrillig    12 年前

    官方的共存解决方案似乎是 Python Launcher for Windows ,第397号政治公众人物 Python 3.3.0 . 安装释放转储 py.exe pyw.exe 发射装置进入 %SYSTEMROOT% ( C:\Windows )然后与 py pyw 分别是剧本。

    为了使用新的启动程序(不需要手动设置与它的关联),请保持“register extensions”选项处于启用状态。我不太清楚为什么,但在我的机器上,PY2.7是默认的(启动程序)。

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

    各种各样的shebang似乎都管用

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

    以及肆无忌惮的虐待

    • #! notepad.exe
        2
  •  37
  •   Alistair Martin    9 年前

    这是我的设置:

    1. 使用 windows installers .
    2. C:\Python34 (默认安装路径)并将python.exe更改为python3.exe
    3. 编辑 your environment variables 包括 C:\Python27\;C:\Python27\Scripts\;C:\Python34\;C:\Python34\Scripts\;

    现在在命令行中,您可以使用 python 2.7和 python3 3.4。

        3
  •  37
  •   AtilioA    6 年前

    两者都可以安装。

    你应该在剧本前面写下:

    #!/bin/env python2.7
    

    或者,最终…

    #!/bin/env python3.6
    

    更新

    在快速搜索 Google ,以下是Windows解决方案:

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

    同样的事情:在你的剧本前面。

        4
  •  33
  •   Ivan Kucerak    8 年前

    从3.3版python引入了用于windows的启动程序实用程序 https://docs.python.org/3/using/windows.html#python-launcher-for-windows .

    因此,要能够使用多个版本的python:

    1. 安装Python2.x(x是您需要的任何版本)
    2. 安装python 3.x(x是您需要的任何版本,还必须有一个版本3.x>=3.3)
    3. 打开 命令提示符
    4. 类型 p- 2 x 启动Python2.x
    5. 类型 p- 3 x 启动Python3.x
        5
  •  9
  •   Community T.Woody    13 年前

    我使用的是shell中的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 把它们放在你路上的某个地方。将首选次要版本(即最新版本)的文件复制到 pythonX.bat . (例如) copy python2.6.bat python2.bat )然后你可以使用 python2 file.py 从任何地方。

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

        6
  •  8
  •   Charif DZ    5 年前

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

    只需重命名其中一个。我把它改名为 python3.exe .

    所以当我跑的时候 python 它将执行 巨蜥 哪一个是2.7 当我跑的时候 python3 它将执行 Python 3.EXE 哪一个是3.6

    enter image description here

        7
  •  7
  •   nem    15 年前

    干得好。。。

    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上都有pythons 2.6.1和3.0.1。如果这对你不起作用,就在这里发布补丁。

        8
  •  4
  •   James McMahon    16 年前

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

    所以如果你在你的路径上指向正确的版本,你就会使用它。记住重新启动命令提示符以使用新的路径设置。

        9
  •  3
  •   Craig McQueen Dr. Watson    13 年前

    python安装通常与 .py , .pyw .pyc 使用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等)并根据需要启动python 2.x或3.x的文件。见 "A Python Launcher for Windows" 有关详细信息的博客文章。

        10
  •  3
  •   Cale Sweeney    5 年前

    试试用水蟒。

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

    conda create --name snakes python=3
    

    它比听起来更简单,看看这里的介绍页: Getting Started with Anaconda

    然后,要处理版本2.x和3.x并排运行的特定问题,请参见 : Managing Python Versions with Anaconda

        11
  •  2
  •   rogerwamba    9 年前

    下面是如何在同一台计算机上运行python 2和3

    1. 安装Python2.x
    2. 安装Python3.x
    3. 启动powershell
    4. 类型 蟒蛇- 2 启动Python2.x
    5. 类型 蟒蛇- 3 启动Python2.x

    这个 用于windows的python启动程序 从3.3版开始就嵌入到了python中,正如2011年单机版首次发布时所承诺的那样:

    Python Launcher for Windows

        12
  •  2
  •   Shreyaa Sridhar    7 年前

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

    现在看起来很简单。

    只是盲目地安装它们。确保得到正确的类型(x64/x32)。 安装时/安装后,请确保将添加到 environment variables .

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

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

    然后转到两个文件夹。

    引导到

    python3.6/Scripts/
    

    并将pip重命名为pip3。

    如果pip3已经存在,请删除pip。这将确保只有皮普会跑下 Python 2 . 您可以通过以下方式进行验证:

    pip --version
    

    如果你想用pip 小精灵 那就用

    pip3 install 
    

    您也可以对python文件和其他文件执行同样的操作。

    干杯!

        13
  •  1
  •   Brian    16 年前

    我认为可以在安装程序中为.py文件设置windows文件关联。不选它,你应该没事的。

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

        14
  •  1
  •   Geo    16 年前

    您应该确保path环境变量不同时包含python.exe文件(添加当前用于日常运行脚本的文件),或者按照knight对批处理文件的建议执行。 除此之外,我不明白为什么不。

    附:我已经安装了2.6作为我的 “小学” python和3.0作为我的 “玩” 蟒蛇。2.6包含在 路径 . 一切正常。

        15
  •  1
  •   nilakantha singh deo    5 年前

    很简单,在安装两个python版本之后,将路径添加到环境变量;请参见 environment variable settings . 然后转到python2和python3文件夹,分别将它们重命名为python2和python3,如图所示 here for python2 here for python3 . 现在在命令中键入python2或python3以使用所需版本,请参见 here .

        16
  •  0
  •   Dan    16 年前

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

        17
  •  0
  •   Davidopopoplis    7 年前

    我刚开始学python。我正在阅读ZedShaw的书“艰难地学习Python”,它需要Python2.x版本,但同时也要学习一个需要Python3.x的类

    这就是我所做的。

    1. 下载Python2.7
    2. 运行power shell(应已安装在Windows上)
    3. 在powershell中运行python(如果无法识别,则转到步骤4)
    4. 只有当powershell无法识别python 2.7时 键入以下内容:

    “[环境]::setenvironmentvariable(”path“,”$env:path;c:\ python27“,”user“)” (无外部引用)

    1. 现在输入python,您会看到它说python 2.7blah blah blah

    现在对于python 3.x

    很简单,python 3.x下载附带python for windows应用程序。所以只需将python for windows应用程序固定到任务栏,或者创建桌面快捷方式,就完成了!

    打开python for windows for 3.x

    为python 2.x打开powershell

    我希望这有帮助!

        18
  •  0
  •   FearlessFuture    6 年前

    嗯..我现在下载Python3.6.5 for Windows https://www.python.org/downloads/release/python-365/ 确保安装了发射器。然后,我按照使用Python2和Python3的说明进行操作。重新启动命令提示符,然后使用 py -2.7 使用python 2和 py py -3.6 使用Python3。你也可以使用 pip2 对于python 2 pip 匹普 对于python 3 匹普 .