如何更新 Python?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15102943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:20:20  来源:igfitidea点击:

How to update Python?

pythonpython-2.7installationupgradewindows-7-x64

提问by Coldblackice

I have version 2.7 installed from early 2012. I can't find any consensus on whether I should completely uninstall and wipe this version before putting on the latest version.

我从 2012 年初安装了 2.7 版。我无法就是否应该在安装最新版本之前完全卸载并擦除此版本达成共识。

"Soft"-removing old versions? Hard-removing/wiping old versions? Installing over top?

“软”-删除旧版本?硬删除/擦除旧版本?安装在顶部?

I've seen somewhere a special install/upgrade process using a "segmenting" method of Python installations, keeping different versions separate and apart, but functional. Not sure if this is the standard, de facto way.

我在某处看到过使用 Python 安装的“分段”方法的特殊安装/升级过程,将不同的版本分开和分开,但功能正常。不确定这是否是标准的、事实上的方式。

I also wonder if Revo gets too overzealous and may cause issues with wiping out still-needed remnants, like environment/PATH variables.

我还想知道 Revo 是否过于热心,可能会导致清除仍然需要的残余物(如环境/PATH 变量)的问题。

(Win7 x64, 32-bit Python)

(Win7 x64,32 位 Python)

采纳答案by Mark Mikofski

UPDATE更新:2018-07-06

This post is now nearly 5 years old! Python-2.7 will stop receiving official updates from python.org in 2020. Also, Python-3.7 has been released. Check out Python-Futureon how to make your Python-2 code compatible with Python-3. For updating conda, the documentation now recommends using conda update --allin each of your conda environments to update all packages and the Python executable for that version. Also, since they changed their name to Anaconda, I don't know if the Windows registry keys are still the same.

这篇文章现在快5岁了!Python-2.7 将在 2020 年停止接收来自 python.org 的官方更新。此外,Python-3.7 已经发布。查看Python-Future,了解如何使您的 Python-2 代码与 Python-3 兼容。对于更新 conda,文档现在建议conda update --all在您的每个 conda 环境中使用来更新该版本的所有包和 Python 可执行文件。另外,由于他们将名称更改为 Anaconda,我不知道 Windows 注册表项是否仍然相同。

UPDATE更新:2017-03-24

There have been no updates to Python(x,y) since June of 2015, so I think it's safe to assume it has been abandoned.

自 2015 年 6 月以来,Python(x,y) 没有更新,因此我认为可以安全地假设它已被放弃。

UPDATE更新:2016-11-11

As @cxw comments below, these answers are for the same bit-versions, and by bit-version I mean 64-bit vs. 32-bit. For example, these answers would apply to updating from 64-bit Python-2.7.10 to 64-bit Python-2.7.11, ie: the samebit-version. While it is possible to install two different bit versions of Python together, it would require some hacking, so I'll save that exercise for the reader. If you don't want to hack, I suggest that if switching bit-versions, remove the other bit-version first.

正如下面@cxw 评论的那样,这些答案是针对相同的位版本,我所说的位版本是指 64 位与 32 位。例如,这些答案适用于从 64 位 Python-2.7.10 更新到 64 位 Python-2.7.11,相同的位版本。虽然可以同时安装两个不同位版本的 Python,但这需要一些技巧,所以我将把这个练习留给读者。如果你不想破解,我建议如果切换位版本,首先删除另一个位版本。

UPDATES更新:2016-05-16
  • Anacondaand MiniCondacan be used with an existing Python installation by disabling the options to alter the Windows PATHand Registry. After extraction, create a symlink to condain your binor install conda from PyPI. Then create another symlink called conda-activateto activatein the Anaconda/Miniconda root bin folder. Now Anaconda/Miniconda is just like Ruby RVM. Just use conda-activate rootto enable Anaconda/Miniconda.
  • Portable Python is no longer being developed or maintained.
  • 通过禁用更改 Windows和注册表的选项,AnacondaMiniConda可以与现有的 Python 安装一起使用PATH。提取后,conda在您的bin或从 PyPI 安装 conda 中创建符号链接。然后创建另一个名为符号链接conda-activateactivate在巨蟒/ Miniconda根bin文件夹。现在 Anaconda/Miniconda 就像 Ruby RVM。仅用于conda-activate root启用 Anaconda/Miniconda。
  • 不再开发或维护便携式 Python。

TL;DR

TL; 博士

  • Using Anaconda or miniconda, then just execute conda update --allto keep each conda environment updated,
  • same majorversion of official Python(e.g.2.7.5), just install over old (e.g.2.7.4),
  • different majorversion of official Python(e.g.3.3), install side-by-side with old, set paths/associations to point to dominant (e.g.2.7), shortcut to other (e.g.in BASH $ ln /c/Python33/python.exe python3).
  • 使用Anaconda 或 miniconda,然后执行conda update --all以保持每个 conda 环境更新,
  • 官方 Python 的相同主要版本例如2.7.5),只需安装旧版本(例如2.7.4),
  • 官方 Python 的不同主要版本例如3.3),与旧版本并排安装,将路径/关联设置为指向主导(例如2.7),快捷方式到其他(例如在 BASH 中$ ln /c/Python33/python.exe python3)。

The answer depends:

答案取决于:

  1. If OP has 2.7.x and wants to install newer version of 2.7.x, then

    • if using MSI installerfrom the official Python website, just install over old version, installer will issue warning that it will remove and replace the older version; looking in "installed programs" in "control panel" before and after confirms that the old version has been replaced by the new version; newer versions of 2.7.x are backwards compatible so this is completely safe and therefore IMHO multiple versions of 2.7.x should never necessary.
    • if building from source, then you should probably build in a fresh, clean directory, and then point your path to the new build once it passes all tests and you are confident that it has been built successfully, but you may wish to keep the old build around because building from source may occasionally have issues. See my guide for building Python x64 on Windows 7 with SDK 7.0.
    • if installing from a distribution such as Python(x,y), see their website. Python(x,y)has been abandoned.I believe that updates can be handled from within Python(x,y) with their package manager, but updates are also included on their website. I could not find a specific reference so perhaps someone else can speak to this. Similar to ActiveState and probably Enthought, Python (x,y) clearly states it is incompatible with other installations of Python:

      It is recommended to uninstall any other Python distribution before installing Python(x,y)

    • Enthought Canopyuses an MSI and will install either into Program Files\Enthoughtor home\AppData\Local\Enthought\Canopy\Appfor all users or per user respectively. Newer installations are updated by using the built in update tool. See their documentation.
    • ActiveStatealso uses an MSI so newer installations can be installed on top of older ones. See their installation notes.

      Other Python 2.7 Installations On Windows, ActivePython 2.7 cannot coexist with other Python 2.7 installations (for example, a Python 2.7 build from python.org). Uninstall any other Python 2.7 installations before installing ActivePython 2.7.

    • Sagerecommends that you install it into a virtual machine, and provides a Oracle VirtualBox image file that can be used for this purpose. Upgrades are handled internally by issuing the sage -upgradecommand.
    • Anacondacan be updated by using the condacommand:

      conda update --all
      

      Anaconda/Miniconda lets users create environmentsto manage multiple Python versions including Python-2.6, 2.7, 3.3, 3.4 and 3.5. The root Anaconda/Miniconda installations are currently based on either Python-2.7 or Python-3.5.

      Anaconda will likely disrupt any other Python installations. Installation uses MSI installer.[UPDATE: 2016-05-16] Anaconda and Miniconda now use .exeinstallers and provide options to disable Windows PATHand Registry alterations.

      Therefore Anaconda/Miniconda can be installed without disrupting existing Python installations depending on how it was installed and the options that were selected during installation. If the .exeinstaller is used and the options to alter Windows PATHand Registry are notdisabled, then any previous Python installations will be disabled, but simply uninstalling the Anaconda/Miniconda installation should restore the original Python installation, except maybe the Windows Registry Python\PythonCorekeys.

      Anaconda/Miniconda makes the following registry edits regardless of the installation options: HKCU\Software\Python\ContinuumAnalytics\with the following keys: Help, InstallPath, Modulesand PythonPath- official Python registers these keys too, but under Python\PythonCore. Also uninstallation info is registered for Anaconda\Miniconda. Unless you select the "Register with Windows" option during installation, it doesn't create PythonCore, so integrations like Python Tools for Visual Studio do not automatically see Anaconda/Miniconda. If the option to register Anaconda/Miniconda isenabled, then I think your existing Python Windows Registry keys will be altered and uninstallation will probably not restore them.

    • WinPythonupdates, I think, can be handled through the WinPython Control Panel.
    • PortablePythonis no longer being developed. It had no update method. Possibly updates could be unzipped into a fresh directory and then App\lib\site-packagesand App\Scriptscould be copied to the new installation, but if this didn't work then reinstalling all packages might have been necessary. Use pip listto see what packages were installed and their versions. Some were installed by PortablePython. Use easy_install pipto install pip if it wasn't installed.
  2. If OP has 2.7.x and wants to install a different version, e.g.<=2.6.x or >=3.x.x, then installing different versions side-by-side is fine. You must choose which version of Python (if any) to associate with *.pyfiles and which you want on your path, although you should be able to set up shells with different paths if you use BASH. AFAIK 2.7.x is backwards compatible with 2.6.x, so IMHO side-by-side installs is not necessary, however Python-3.x.x is not backwards compatible, so my recommendation would be to put Python-2.7 on your path and have Python-3 be an optional version by creating a shortcut to its executable called python3 (this is a common setup on Linux). The official Python default install path on Windows is

    • C:\Python33 for 3.3.x (latest 2013-07-29)
    • C:\Python32 for 3.2.x
    • &c.
    • C:\Python27 for 2.7.x (latest 2013-07-29)
    • C:\Python26 for 2.6.x
    • &c.
  3. If OP is not updating Python, but merely updating packages, they may wish to look into virtualenvto keep the different versions of packages specific to their development projects separate. Pipis also a great tool to update packages. If packages use binary installers I usually uninstall the old package before installing the new one.

  1. 如果 OP 有 2.7.x 并且想要安装更新版本的 2.7.x,那么

    • 如果使用来自官方 Python 网站的MSI 安装程序,只需安装旧版本,安装程序将发出警告,将删除并替换旧版本;在“控制面板”中查看“已安装的程序”前后确认旧版本已被新版本替换;2.7.x 的较新版本向后兼容,因此这是完全安全的,因此恕我直言,2.7.x 的多个版本应该永远不需要。
    • 如果从源代码构建,那么您可能应该在一个新的、干净的目录中构建,然后在新构建通过所有测试并且您确信它已成功构建后将路径指向新构建,但您可能希望保留旧的构建,因为从源代码构建可能偶尔会出现问题。请参阅我的使用 SDK 7.0 在 Windows 7 上构建 Python x64 的指南
    • 如果从Python(x,y)等发行版安装,请参阅他们的网站。Python(x,y)已被放弃。我相信更新可以通过他们的包管理器从 Python(x,y) 中处理,但更新也包含在他们的网站上。我找不到具体的参考资料,所以也许其他人可以谈谈这个。与 ActiveState 和 Enthought 类似,Python (x,y) 明确指出它与其他 Python 安装不兼容:

      建议在安装 Python(x,y) 之前卸载任何其他 Python 发行版

    • Enthought雨棚采用了微星将安装到任何Program Files\Enthoughthome\AppData\Local\Enthought\Canopy\App所有用户或每个用户分别。使用内置更新工具更新较新的安装。请参阅他们的文档
    • ActiveState还使用 MSI,因此新安装可以安装在旧安装之上。请参阅他们的安装说明

      其他 Python 2.7 安装 在 Windows 上,ActivePython 2.7 不能与其他 Python 2.7 安装共存(例如,来自 python.org 的 Python 2.7 构建)。在安装 ActivePython 2.7 之前卸载任何其他 Python 2.7 安装。

    • Sage建议您将其安装到虚拟机中,并提供可用于此目的的 Oracle VirtualBox 映像文件。通过发出sage -upgrade命令在内部处理升级。
    • 可以使用以下conda命令更新Anaconda

      conda update --all
      

      Anaconda/Miniconda 允许用户创建环境来管理多个 Python 版本,包括 Python- 2.6、2.7、3.3、3.4和 3.5。根 Anaconda/Miniconda 安装当前基于 Python-2.7 或 Python-3.5。

      Anaconda 可能会破坏任何其他 Python 安装。安装使用 MSI 安装程序。[更新:2016-05-16] Anaconda 和 Miniconda 现在使用.exe安装程序并提供禁用 WindowsPATH和注册表更改的选项。

      因此,根据安装方式和安装过程中选择的选项,可以在不中断现有 Python 安装的情况下安装 Anaconda/Miniconda。如果使用.exe安装程序PATH并且禁用更改 Windows和注册表的选项,则任何以前的 Python 安装都将被禁用,但只需卸载 Anaconda/Miniconda 安装即可恢复原始 Python 安装,Windows 注册表Python\PythonCore项除外。

      蟒蛇/ Miniconda使得下面的注册表编辑无论安装选项:HKCU\Software\Python\ContinuumAnalytics\使用下列按键:HelpInstallPathModulesPythonPath- Python官方注册过这些按键,但下Python\PythonCore。还为 Anaconda\Miniconda 注册了卸载信息。除非您在安装过程中选择“Register with Windows”选项,否则它不会创建PythonCore,因此像 Python Tools for Visual Studio 这样的集成不会自动看到 Anaconda/Miniconda。如果注册蟒蛇/ Miniconda选项激活,那么我认为您现有的Python Windows注册表项将被改变和卸载可能不会恢复它们。

    • 我认为,WinPython更新可以通过WinPython 控制面板进行处理。
    • PortablePython不再被开发它没有更新方法。可能更新可以解压缩到一个新的目录,然后App\lib\site-packagesApp\Scripts可以复制到新安装的,但如果没有工作,然后重新安装所有的包可能是必要的。使用pip list查看包安装了什么,它们的版本。有些是由 PortablePython 安装的。使用easy_install pip如果未安装它安装点子。
  2. 如果 OP 有 2.7.x 并且想要安装不同的版本,例如<=2.6.x 或 >=3.xx,那么并排安装不同的版本是可以的。您必须选择要与*.py文件关联的 Python 版本(如果有)以及您想要的路径,但如果您使用BASH,您应该能够设置具有不同路径的 shell 。AFAIK 2.7.x 向后兼容 2.6.x,所以恕我直言并排安装不是必需的,但是 Python-3.xx 不向后兼容,所以我的建议是将 Python-2.7 放在你的路径上并有通过创建名为 python3 的可执行文件的快捷方式,Python-3 成为可选版本(这是 Linux 上的常见设置)。Windows 上的官方 Python 默认安装路径是

    • C:\Python33 for 3.3.x (最新 2013-07-29)
    • C:\Python32 for 3.2.x
    • &C。
    • C:\Python27 for 2.7.x (最新 2013-07-29)
    • C:\Python26 for 2.6.x
    • &C。
  3. 如果 OP 不更新 Python,而只是更新包,他们可能希望查看virtualenv以将特定于其开发项目的不同版本的包分开。Pip也是一个很好的更新包的工具。如果软件包使用二进制安装程序,我通常会在安装新软件包之前先卸载旧软件包。

I hope this clears up any confusion.

我希望这能消除任何困惑。

回答by Zack

I have always just installed the new version on top and never had any issues. Do make sure that your path is updated to point to the new version though.

我一直只是在顶部安装新版本,从未遇到任何问题。不过,请确保您的路径已更新以指向新版本。

回答by Wei Yen

The best solution is to install the different Python versions in multiple paths.

最好的解决方案是在多个路径中安装不同的 Python 版本。

eg. C:\Python27 for 2.7, and C:\Python33 for 3.3.

例如。C:\Python27 用于 2.7,C:\Python33 用于 3.3。

Read this for more info: How to run multiple Python versions on Windows

阅读本文了解更多信息:如何在 Windows 上运行多个 Python 版本

回答by ivan_pozdeev

  • Official Python .msi installers are designed to replace:

    • any previous micro release (in x.y.z, zis "micro") because they are guaranteed to be backward-compatible and binary-compatible
    • a "snapshot" (built from source) installation with any micro version
  • A snapshot installer is designed to replace any snapshot with a lower micro version.

  • 官方 Python .msi 安装程序旨在替换:

    • 任何以前的微版本(在xyz 中z是“微”),因为它们保证向后兼容和二进制兼容
    • 任何微型版本的“快照”(从源代码构建)安装
  • 快照安装程序旨在用较低的微版本替换任何快照。

(See responsible code for 2.x, for 3.x)

(请参阅2.x 的负责代码3.x 的负责代码

Any other versions are not necessarily compatible and are thus installed alongside the existing one. If you wish to uninstall the old version, you'll need to do that manually. And also uninstall any 3rd-party modules you had for it:

任何其他版本不一定兼容,因此与现有版本一起安装。如果您想卸载旧版本,则需要手动执行此操作。并卸载您拥有的任何 3rd-party 模块:

  • If you installed any modules from bdist_wininstpackages (Windows .exes), uninstall them before uninstalling the version, or the uninstaller might not work correctly if it has custom logic
  • modules installed with setuptools/pipthat reside in Lib\site-packagescan just be deleted afterwards
  • packages that you installed per-user, if any, reside in %APPDATA%/Python/PythonXY/site-packagesand can likewise be deleted
  • 如果您从bdist_wininst软件包 (Windows .exes)安装了任何模块,请在卸载版本之前卸载它们,否则如果具有自定义逻辑,卸载程序可能无法正常工作
  • 与安装的模块setuptools/pip中驻留Lib\site-packages可以只在事后删除
  • 您为每个用户安装的软件包(如果有)驻留在其中%APPDATA%/Python/PythonXY/site-packages,同样可以删除