使用 Chocolatey Windows 包管理器安装/升级 Python
假设你有 Python 2.7.16:
C:\Windows\system32>python --version
python2 2.7.16
...并且您想升级到(现在是当前的)3.x.y 版本。有一种使用 Windows 包管理工具并行安装 Python 3.x.y 的简单方法。
现在现代 Windows 有包管理,就像 Debian Linux 发行版有 apt-get,RedHat 有 dnf:我们可以让它为我们工作!它叫做巧克力。
什么是巧克力味?
Chocolatey 是一个可编写脚本的命令行工具,它基于 .NET 4.0 和嵌入到 Visual Studio 中的 nuget 包管理器。
如果您想了解 Chocolatey 以及为什么要使用它,这里的一些人可能会觉得这特别有用,请转到 https://chocolatey.org/docs/why
安装 Chocolatey
>
要获得 Chocolatey 包管理器,您需要遵循https://chocolatey.org/docs/installation#installing-chocolatey 中描述的流程,
我会在这里为你总结一下。基本上有两种选择:使用 cmd 提示符,或使用 PowerShell 提示符。
CMD 提示 Chocolatey 安装
启动管理命令提示符。在 Windows 10 上,执行此操作:
-
Windows+R
- 键入 cmd
- 按 ctrl+shift+Enter
如果您没有系统管理员权限,请转至Chocolatey website。您可能并不完全不走运,并且可以执行有限的本地安装,但我不会在这里介绍。
- 将下面的字符串复制到命令提示符中,然后输入 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.x.y(在撰写本文时为 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
...会成功的。玩 Python 快乐!