如何在Windows 10中升级Python安装?


112

我在我的一个LAB工作站上安装了Python 2.7.11。我想将Python至少升级到3.5。

我该怎么办?我是否应该完全卸载2.7.11而不是安装新的?有办法更新吗?更新是个好主意吗?


也许这个较旧的帖子可以为您提供帮助?stackoverflow.com/questions/15102943/how-to-update-python Br。
MikeL

python2和python3应该被视为不同的语言,例如C#与C ++是不同的
WhatsThePoint

Answers:


97

Python的每个次要版本(即任何3.x和2.x版本)都将与计算机上的其他版本并排安装。仅补丁程序版本将升级现有安装。

因此,如果您想保留已安装的Python 2.7,请放任它并使用安装程序安装新版本。如果要摆脱Python 2.7,可以在安装较新版本之前或之后将其卸载-两者没有什么区别。

当前的Python 3安装带有py.exe启动器,默认情况下安装在系统目录中。这使得它可以从PATH中使用,因此您只需使用py而不是python作为命令就可以从任何shell自动运行它。这避免了您必须自己将当前的Python安装放入PATH。这样,您可以轻松地并排安装多个Python,而不会彼此干扰。运行时,仅使用py script.py而不是python script.py使用启动器。您也可以使用例如指定一个版本py -3py -3.6启动一个特定版本,否则启动程序将使用当前默认值(通常是最新的3.x)。

如果您决定保留Python 2.7安装,则还可以使用启动器运行Python 2脚本(这些语法通常与Python 3不兼容)。只需用于py -2 script.py启动脚本即可。


对于PyPI软件包,每个Python安装都带有其自己的文件夹,模块被安装到该文件夹​​中。因此,如果您安装新版本并且要使用为先前版本安装的模块,则必须为新版本首先安装它们。当前版本的安装程序也提供您安装pip; 默认情况下已启用它,因此您已经可以pip进行每次安装。除非您明确将Python安装添加到PATH,否则您将无法使用pip。幸运的是,您也可以py.exe为此使用启动器:py -m pipruns pip。因此,例如,要为Python 3.6安装Beautiful Soup,可以运行py -3.6 -m pip install beautifulsoup4


1
您将如何建议在我当前用于脚本开发的工作站上执行此操作?顺便说一句,我有一个3.5.2(Windows 7 64位)
Moshe S.

1
只需并排安装新版本。无需删除旧版本。使用旧版本甚至可以帮助您将现有脚本从Python 2迁移到Python 3,因此您仍然可以运行原始脚本。

1
安装程序将为您提供一个选项来为您设置PATH,因此您可能不需要手动执行此操作。是的,您的IDE需要更新。

1
py -3.7-64或者py -3.7-32
noobninja

1
注意: pyWindowsPython启动器。或参见定义它的PEP 397。没有为Linux / Mac安装它。
LightCC

39

使用Chocolatey Windows软件包管理器安装/升级Python

假设您有Python 2.7.16:

    C:\Windows\system32>python --version
    python2 2.7.16

...并且您想升级到(现在)3.xy版本。有一种简单的方法可以使用Windows软件包管理工具来并行安装Python3.xy。

现在,现代Windows具有包管理功能,就像Debian Linux发行版具有apt-get一样,而RedHat具有dnf:我们可以为我们使用它了!叫做 Chocolatey

什么是巧克力味?

Chocolatey是一个可脚本化的命令行工具,基于.NET 4.0和nuget程序包管理器引入了Visual Studio。

如果您想了解Chocolatey以及为什么要使用它,某些在这里阅读本文的人可能会发现它特别有用,请访问https://chocolatey.org/docs/why

安装Chocolatey

要获取Chocolatey Package Manager,请按照https://chocolatey.org/docs/installation#installing-chocolatey所述的过程进行操作,

我将在这里为您总结。基本上有两个选项:使用cmd提示符或使用PowerShell提示符。

CMD提示巧克力般的安装

启动管理命令提示符。在Windows 10上,执行以下操作:

  • Windows+R
  • 输入cmd
  • ctrl+ shift+Enter

如果您没有系统管理员权限,请访问Chocolatey网站。您可能并不完全走运,可以执行有限的本地安装,但在此不做介绍。

  • 将以下字符串复制到命令提示符中,然后输入Enter:
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

Chocolatey将为您下载和安装,如下所示:

Getting latest version of the Chocolatey package for download.
Getting Chocolatey from https://chocolatey.org/api/v2/package/chocolatey/0.10.11.
Downloading 7-Zip commandline tool prior to extraction.
Extracting C:\Users\blahblahblah\AppData\Local\Temp\chocolatey\chocInstall\chocolatey.zip to C:\Users\blahblahblah\AppData\Local\Temp\chocolatey\chocInstall...
Installing chocolatey on this machine
Creating ChocolateyInstall as an environment variable (targeting 'Machine')
  Setting ChocolateyInstall to 'C:\ProgramData\chocolatey'
WARNING: It's very likely you will need to close and reopen your shell
  before you can use choco.
Restricting write permissions to Administrators
We are setting up the Chocolatey package repository.
The packages themselves go to 'C:\ProgramData\chocolatey\lib'
  (i.e. C:\ProgramData\chocolatey\lib\yourPackageName).
A shim file for the command line goes to 'C:\ProgramData\chocolatey\bin'
  and points to an executable in 'C:\ProgramData\chocolatey\lib\yourPackageName'.

Creating Chocolatey folders if they do not already exist.

WARNING: You can safely ignore errors related to missing log files when
  upgrading from a version of Chocolatey less than 0.9.9.
  'Batch file could not be found' is also safe to ignore.
  'The system cannot find the file specified' - also safe.
chocolatey.nupkg file not installed in lib.
 Attempting to locate it from bootstrapper.
PATH environment variable does not have C:\ProgramData\chocolatey\bin in it. Adding...
WARNING: Not setting tab completion: Profile file does not exist at 'C:\Users\blahblahblah\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'.
Chocolatey (choco.exe) is now ready.
You can call choco from anywhere, command line or powershell by typing choco.
Run choco /? for a list of functions.
You may need to shut down and restart powershell and/or consoles
 first prior to using choco.
Ensuring chocolatey commands are on the path
Ensuring chocolatey.nupkg is in the lib folder

要么退出的CMD提示或键入以下命令以重新加载环境变量:

refreshenv

PowerShell Chocolatey安装

如果您更喜欢PowerShell而不是cmd提示符,则可以从那里直接执行此操作,但是必须告诉PowerShell使用正确的脚本执行策略运行才能使其正常工作。在Windows 10上,我发现最简单的方法是在Windows按钮旁边的Cortana搜索栏中键入以下内容:

PowerShell.exe

接下来,右键单击弹出菜单中的“最佳匹配”选项,然后选择“以管理员身份运行”

现在您已经在PowerShell中,希望以管理员权限运行,执行以下命令以安装Chocolatey:

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

PowerShell将为您下载Chocolatey并启动安装。只需要一点时间。它看起来与CMD安装完全一样,可能会保存一些精美的彩色文本。

无论是退出的PowerShell或键入以下命令来重新加载环境变量:

refreshenv

升级Python

无论您使用PowerShell还是cmd提示符,choco命令都是相同的。按照上述说明启动您喜欢的游戏。我将使用管理员cmd提示符:

C:\WINDOWS\system32>choco upgrade python -y

本质上,Chocolatey会告诉您“嘿,未安装Python”,因为您来自2.7.x,它将2.7版本视为完全独立。它只会为您提供最新版本3.xy(在撰写本文时为3.7.2,但几个月后会更改):

Chocolatey v0.10.11
Upgrading the following packages:
python
By upgrading you accept licenses for the packages.
python is not installed. Installing...

python3 v3.x.y [Approved]
python3 package files upgrade completed. Performing other installation steps.
Installing 64-bit python3...
python3 has been installed.
Installed to: 'C:\Python37' 
  python3 can be automatically uninstalled.
Environment Vars (like PATH) have changed. Close/reopen your shell to
 see the changes (or in powershell/cmd.exe just type `refreshenv`).
 The upgrade of python3 was successful.
  Software installed as 'exe', install location is likely default.

python v3.x.y [Approved]
python package files upgrade completed. Performing other installation steps.
 The upgrade of python was successful.
  Software install location not explicitly set, could be in package or
  default install location if installer.

Chocolatey upgraded 2/2 packages.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

要么退出CMD的出/ Powershell的提示和重新输入,或者使用然后键入refreshenv PY --version

C:\Windows\System32>refreshenv
Refreshing environment variables from registry for cmd.exe. Please wait...Finished..

C:\Windows\system32>py --version
Python 3.7.2

请注意,当您在命令行中键入Python时,最新的Python安装现在将接管。您可以使用以下命令来运行任一版本:

py -2
Python 2.7.16 (v2.7.16:413a49145e, Mar  4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

C:\>py -3
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>exit()
C:\>

从这里,我建议您使用Python pip实用程序安装所需的任何软件包。例如,假设您要安装Flask。下面的命令首先升级pip,然后安装Flask

C:\>py -3 -m pip install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 1.6MB/s
Installing collected packages: pip
  Found existing installation: pip 18.1
    Uninstalling pip-18.1:
      Successfully uninstalled pip-18.1
Successfully installed pip-19.0.3

c:\>py -3 -m pip install Flask

...会成功的 快乐的Pythoning!


答案的简短版本是先安装Chocolatey,然后从命令提示符处刷新并执行choco install python,它将安装最新版本的python。在cmd中再次执行refreshenv,您应该从cmd运行最新的python。
塞巴斯蒂安

“在CMD和POWERSHELL中获取此错误异常,使用参数” 1“调用” DownloadString“:”基础连接已关闭:发送中发生意外错误。“ 在第1行:char:104 + Set-ExecutionPolicy Bypass -Scope Process -Force; iex((New-Object System.Net.WebClient).DownloadString <<<<(' Chocolatey.org/install.ps1'))+ CategoryInfo:未指定:(:) [],MethodInvocationException + FullyQualifiedErrorId:DotNetMethodException
可信任

我有Windows 7的任何帮助如何从python2升级到python 3似乎chocoaltely不起作用。
信托

38

如果要将任何3.xy升级到3.xz(补丁)Python版本,只需转到Python下载页面获取最新版本并开始安装。由于您已经在计算机安装程序中安装了Python,因此会提示您“立即升级”。单击该按钮,它将用新版本替换现有版本。安装后,您还必须重新启动计算机。

在此处输入图片说明

如果您是从3.x升级到3.y(较小),则系统将提示您“立即安装”。在这种情况下,您不是要升级,而是要安装新版本的Python。您的计算机上可以安装多个版本。它们将位于不同的目录中。如果您的计算机上有多个Python版本,则需要使用py lanucher启动特定版本的Python。

例如:

py -3.7

要么

py -3.8

确保在计算机上安装了py启动器。如果您使用Windows Installer的默认设置,它将自动安装。如果单击安装窗口上的“定制安装”链接,则始终可以检查它。

如果您的计算机上安装了多个Python版本,并且您有一个使用虚拟环境(例如(venv))使用Python早期版本的项目,则可以使用以下命令在该venv中升级Python:

python -m venv --upgrade“您的虚拟环境路径”

例如,我在./venv虚拟环境中有Python 3.7,并且想将venv升级到Python 3.8,我会按照以下说明进行操作

python -m venv --upgrade ./venv


1
@MosheS。您可以使用以下命令获取过期软件包的列表:pip list --outdated,然后通过python -m pip install
--upgrade

5
从Windows 10上的3.7.2迁移到3.7.4(64位),我看不到,看到“立即安装”,在这里看到“立即升级”
dumbledad

3
我刚刚从3.7.3迁移到3.7.5,并收到了“立即升级”消息。如果我尝试从3.7.3升级到3.8,则将显示“立即安装”。
Tiago Martins Peres李大仁

1
@Suncatcher您说的对,这就是我描述的“如果您想从3.x升级到3.y然后...”部分。请阅读以上内容。
弗拉德·贝兹登

1
@Suncatcher,谢谢您的反馈。我更新了说明。
弗拉德·贝兹登

4

Python 2.x和Python 3.x是不同的。如果您想下载Python 2的较新版本,则只需下载并安装较新的版本。

如果要安装Python 3,则可以单独安装Python 3,然后在控制面板>所有控制面板项目>系统>高级系统设置>环境变量中将Python 2.x的路径更改为Python3.x。


我宁愿完全删除Python 2.7.11,因为我不打算支持它。因此,我应该将其完全删除。对 ?“程序和功能”没有显示Python,所以我不知道如何删除它。我应该使用点子吗?仅删除Python文件夹是一个好主意吗?与它一起安装的所有旧软件包(如果有的话)又如何呢,我又如何卸载它们呢?
Moshe S.

@MosheS。我不认为它确实占用了那么多空间,但是如果您确实想要清理,那就一定要清理。它们应该都在同一个文件夹中,您可以从控制面板的“程序和功能”中卸载它们。我的Python 2.7和3.4都在其中。从那里卸载将删除所有软件包。如果您确实找不到它,可以尝试删除整个文件夹。当您下载Python 3时,如果它不起作用,请按照我上面的回答编辑路径,这很可能是主要问题(如果有的话)。
朱利安·陈

3

在2019中,您可以使用Chocolatey安装。打开您的cmd或powershell,输入“ choco install python”。


-1

安装最新版本的Python(如果已卸载其他版本)的最简单方法是键入PythonPowerShell。

这将打开Microsoft Store,然后单击Get按钮。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.