如何更新Python?

144
我安装了2012年早期的版本2.7。我找不到任何共识,关于是否应该在安装最新版本之前完全卸载和清除此版本。
是软删除旧版本?还是硬删除/清除旧版本?覆盖安装?
我曾经看到过一种特殊的安装/升级过程,使用Python安装的“分段”方法,将不同版本分开并保持独立但功能正常。不确定是否为标准的事实方式。
我也想知道Revo是否过于热衷,可能会导致擦除仍然需要的残留物,例如环境/PATH变量。
(Win7 x64,32位Python)

2
2.7仍然是最新版本——至少在2.x系列中是这样。3.x不向后兼容。如果您想升级到当前的2.7版本,我建议直接覆盖安装。 - Eevee
4个回答

109
更新

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

更新

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

更新

@cxw下面的评论所述,这些答案是针对相同位版本而言,而我所说的位版本是指64位与32位。例如,这些答案适用于从64位Python-2.7.10更新到64位Python-2.7.11,即相同的位版本。虽然可以安装两个不同位版本的Python,但需要进行一些黑客攻击,因此我建议读者在没有进行黑客攻击的情况下,如果要切换位版本,则应先删除其他位版本。

更新内容 以下是您需要翻译的内容:
  • AnacondaMiniConda 可以与现有的 Python 安装一起使用,只需禁用更改 Windows PATH 和注册表的选项。提取后,在您的 bin 中创建到 conda 的符号链接,或从 PyPI 安装 conda。然后在 Anaconda/Miniconda 根目录的 bin 文件夹中创建另一个名为 conda-activate 的符号链接,指向 activate。现在 Anaconda/Miniconda 就像 Ruby RVM 一样方便使用。只需使用 conda-activate root 启用 Anaconda/Miniconda。
  • Portable Python 不再被开发或维护。

TL;DR

  • 如果使用Anaconda或miniconda,只需执行conda update --all来保持每个conda环境的更新。
  • 相同的主要版本official Python(例如2.7.5),只需安装在旧版本上(例如2.7.4)。
  • 不同的主要版本official Python(例如3.3),需要与旧版本并存,并设置路径/关联以指向主导版本(例如2.7),缩短其他版本的时间(例如在BASH中$ ln /c/Python33/python.exe python3)。

答案取决于:

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

    • if using MSI installer from 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 Canopy uses an MSI and will install either into Program Files\Enthought or home\AppData\Local\Enthought\Canopy\App for all users or per user respectively. Newer installations are updated by using the built in update tool. See their documentation.
    • ActiveState also 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.

    • Sage recommends 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 -upgrade command.
    • Anaconda can be updated by using the conda command:

      conda update --all
      

      Anaconda/Miniconda lets users create environments to 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 .exe installers and provide options to disable Windows PATH and 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 .exe installer is used and the options to alter Windows PATH and Registry are not disabled, 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\PythonCore keys.

      Anaconda/Miniconda makes the following registry edits regardless of the installation options: HKCU\Software\Python\ContinuumAnalytics\ with the following keys: Help, InstallPath, Modules and 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 is enabled, then I think your existing Python Windows Registry keys will be altered and uninstallation will probably not restore them.

    • WinPython updates, I think, can be handled through the WinPython Control Panel.
    • PortablePython is no longer being developed. It had no update method. Possibly updates could be unzipped into a fresh directory and then App\lib\site-packages and App\Scripts could be copied to the new installation, but if this didn't work then reinstalling all packages might have been necessary. Use pip list to see what packages were installed and their versions. Some were installed by PortablePython. Use easy_install pip to 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 *.py files 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 virtualenv to keep the different versions of packages specific to their development projects separate. Pip is also a great tool to update packages. If packages use binary installers I usually uninstall the old package before installing the new one.

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


2
嗨马克,我有一个新手问题。您认为从2.7.x升级到3.3.x实际上很重要吗?我开始使用Python,并安装了旧版本,但由于我是初学者,我想学习最新版本(除非只有非常微小的区别)。您对此有何看法?感谢您的帮助。 - Matteo
8
我会同时使用两个版本。如果你正在使用Windows系统,你会在根目录下看到C:\Python27C:\Python33;如果你是在Mac上,你会看到/Library/Frameworks/Python/Versions/2.7.x/Library/Frameworks/Python/Versions/3.3.x;如果你是在Linux上,你可能已经安装了这两个版本。传统的做法是使用Python-2.7,并将Python-3.3的二进制文件建立符号链接到python33。另请参见Python2orPython3Python 2 vs Python 3。我个人太懒了,不愿意切换到3.3。 - Mark Mikofski
2
感谢您提供详细的答案!在使用2.7版本时,如果一个是32位,另一个是64位,覆盖安装就无法正常工作。我最终在“程序和功能”中得到了两个指向C:\Python27的条目。您是否愿意在您的答案中补充一点,即用户应该只将32位版本覆盖安装到32位系统中,将64位版本覆盖安装到64位系统中?谢谢! - cxw
1
@cxw 感谢您的反馈。如果您确实想要同时拥有32位和64位版本,有几种方法可供选择。其中最简单的可能是使用所谓的"管理员"安装将32位版本从命令行安装到C:\Python27_x86中:msiexec /a "python-2.7.11.msi" TARGETDIR="C:\Python27_x86" /qn /l*v python27_x86_install.log。另一种选择是从源代码构建,或尝试Bootstrap Python - Mark Mikofski
1
@MarkMikofski 是的,之前安装的包也被保留了。我确认这一点适用于2.7.15安装程序(我将我的Python从2.7.6升级到2.7.15)。 - XXN
显示剩余4条评论

5
最佳解决方案是在多个路径中安装不同版本的Python。
例如,C:\Python27 是2.7版本,C:\Python33 是3.3版本。
阅读此内容以获取更多信息:如何在Windows上运行多个Python版本

这是我如何操作的。 我同时运行32位和64位版本的Python 2.7.xx、3.5.xx和IronPython 2.7.xx。 我使用Eclipse进行开发,并在调试时选择Python路径。 运行脚本时,我只需设置要使用的解释器版本路径。将3.5.0更新为3.5.3提供了一个简单的更新选项,而不是全新安装。 - Bill Kidd

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

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

(请参阅2.x的相关代码3.x的相关代码

其他版本不一定兼容,因此如果您想卸载旧版本,则需要手动执行。并且,还要卸载任何为其编写的第三方模块:

  • 如果您安装了任何来自bdist_wininst包(Windows .exes)的模块,请在卸载版本之前卸载它们,否则如果它具有自定义逻辑,则卸载程序可能无法正常工作
  • 使用setuptools/pip安装的位于Lib\site-packages中的模块可以在卸载后直接删除
  • 按用户安装的软件包(如果有)位于%APPDATA%/Python/PythonXY/site-packages,也可以删除

1

我一直只是在旧版本的基础上安装新版本,从未遇到任何问题。但请确保您的路径已更新以指向新版本。


2
当您进行覆盖安装时,它是否保留我下载的外部模块,如pygame、bs4、lxml等? - user3917838

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接