如何升级 Windows 10 中的 Python 安装?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45137395/
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
How do I upgrade the Python installation in Windows 10?
提问by Moshe S.
I have a Python 2.7.11 installed on one of my LAB stations. I would like to upgrade Python to at least 3.5.
我在我的一个实验室站上安装了 Python 2.7.11。我想将 Python 升级到至少 3.5。
How should I do that ? Should I prefer to completely uninstall 2.7.11 and than install the new one ? Is there a way to update it ? Is an update a good idea ?
我该怎么做?我应该更喜欢完全卸载 2.7.11 而不是安装新的吗?有没有办法更新它?更新是个好主意吗?
采纳答案by poke
Every minor version of Python, that is any 3.x and 2.x version, will install side-by-side with other versions on your computer. Only patch versions will upgrade existing installations.
Python 的每个次要版本,即任何 3.x 和 2.x 版本,都将与您计算机上的其他版本并行安装。只有补丁版本会升级现有安装。
So if you want to keep your installed Python 2.7 around, then just let it and install a new version using the installer. If you want to get rid of Python 2.7, you can uninstall it before or after installing a newer version—there is no difference to this.
因此,如果您想保留已安装的 Python 2.7,那么只需让它使用安装程序安装新版本即可。如果你想摆脱 Python 2.7,你可以在安装新版本之前或之后卸载它——这没有区别。
Current Python 3 installations come with the py.exe
launcher, which by default is installed into the system directory. This makes it available from the PATH, so you can automatically run it from any shell just by using py
instead of python
as the command. This avoids you having to put the current Python installation into PATH yourself. That way, you can easily have multiple Python installations side-by-side without them interfering with each other. When running, just use py script.py
instead of python script.py
to use the launcher. You can also specify a version using for example py -3
or py -3.6
to launch a specific version, otherwise the launcher will use the current default (which will usually be the latest 3.x).
当前的 Python 3 安装带有py.exe
启动器,默认情况下安装到系统目录中。这使得它可以从 PATH 中使用,因此您可以从任何 shell 中自动运行它,只需使用py
而不是python
作为命令。这避免了您必须自己将当前的 Python 安装放入 PATH 中。这样,您可以轻松地并排安装多个 Python,而不会相互干扰。运行时,只需使用py script.py
而不是python script.py
使用启动器。您还可以使用例如指定版本py -3
或py -3.6
启动特定版本,否则启动器将使用当前默认值(通常是最新的 3.x)。
Using the launcher, you can also run Python 2 scripts (which are often syntax incompatible to Python 3), if you decide to keep your Python 2.7 installation. Just use py -2 script.py
to launch a script.
如果您决定保留 Python 2.7 安装,还可以使用启动器运行 Python 2 脚本(这些脚本通常与 Python 3 的语法不兼容)。仅用于py -2 script.py
启动脚本。
As for PyPI packages, every Python installation comes with its own folder where modules are installed into. So if you install a new version and you want to use modules you installed for a previous version, you will have to install them first for the new version. Current versions of the installer also offer you to install pip
; it's enabled by default, so you already have pip
for every installation. Unless you explicitly add a Python installation to the PATH, you cannot just use pip
though. Luckily, you can also simply use the py.exe
launcher for this: py -m pip
runs pip
. So for example to install Beautiful Soup for Python 3.6, you could run py -3.6 -m pip install beautifulsoup4
.
至于 PyPI 包,每个 Python 安装都有自己的文件夹,模块安装到其中。因此,如果您安装新版本并希望使用为以前版本安装的模块,则必须先为新版本安装它们。安装程序的当前版本还为您提供安装pip
;它默认启用,因此您pip
在每次安装时都已拥有。除非您明确地将 Python 安装添加到 PATH,否则您不能只是使用pip
。幸运的是,您也可以简单地使用py.exe
启动器:py -m pip
runs pip
。因此,例如要为 Python 3.6 安装 Beautiful Soup,您可以运行py -3.6 -m pip install beautifulsoup4
.
回答by Julian Chan
Python 2.x and Python 3.x are different. If you would like to download a newer version of Python 2, you could just download and install the newer version.
Python 2.x 和 Python 3.x 是不同的。如果您想下载较新版本的 Python 2,您只需下载并安装较新版本即可。
If you want to install Python 3, you could install Python 3 separately then change the path for Python 2.x to Python 3.x in Control Panel > All Control Panel Items > System > Advanced System Settings > Environment Variables.
如果要安装 Python 3,可以单独安装 Python 3,然后在控制面板 > 所有控制面板项目 > 系统 > 高级系统设置 > 环境变量中将 Python 2.x 的路径更改为 Python 3.x。
回答by roscoe_x
In 2019, you can install using chocolatey. Open your cmd or powershell, type "choco install python".
在 2019 年,您可以使用Chocolatey进行安装。打开你的 cmd 或 powershell,输入“choco install python”。
回答by ggariepy
Installing/Upgrading Python Using the Chocolatey Windows Package Manager
使用 Chocolatey Windows 包管理器安装/升级 Python
Let's say you have Python 2.7.16:
假设您有 Python 2.7.16:
C:\Windows\system32>python --version
python2 2.7.16
...and you want to upgrade to the (now current) 3.x.y version. There is a simple way to install a parallelinstallation of Python 3.x.y using a Windows package management tool.
...并且您想升级到(现在是最新的)3.xy 版本。有一种使用 Windows 包管理工具并行安装 Python 3.xy的简单方法。
Now that modern Windows has package management, just like Debian Linux distributions have apt-get, and RedHat has dnf: we can put it to work for us! It's called Chocolatey.
现在现代 Windows 有包管理,就像 Debian Linux 发行版有 apt-get 一样,RedHat 有 dnf:我们可以让它为我们工作!它叫做巧克力味。
What's Chocolatey?
什么是巧克力味?
Chocolatey is a scriptable, command line tool that is based on .NET 4.0 and the nuget package manager baked into Visual Studio.
Chocolatey 是一个可编写脚本的命令行工具,它基于 .NET 4.0 和融入 Visual Studio 的 nuget 包管理器。
If you want to learn about Chocolatey and why to use it, which some here reading this might find particularly useful, go to https://chocolatey.org/docs/why
如果您想了解 Chocolatey 以及为什么要使用它,这里的一些人可能会觉得特别有用,请访问https://chocolatey.org/docs/why
Installing Chocolatey
安装巧克力
To get the Chocolatey Package Manager, you follow a process that is described at https://chocolatey.org/docs/installation#installing-chocolatey,
要获取 Chocolatey 包管理器,请遵循https://chocolatey.org/docs/installation#installing-chocolatey 中描述的过程,
I'll summarize it for you here. There are basically two options: using the cmd prompt, or using the PowerShell prompt.
我在这里为你总结一下。基本上有两种选择:使用 cmd 提示符,或使用 PowerShell 提示符。
CMD Prompt Chocolatey Installation
CMD 提示巧克力安装
Launch an administrativecommand prompt. On Windows 10, to do this:
启动管理命令提示符。在 Windows 10 上,要执行此操作:
- Windows+R
- Type cmd
- Press ctrl+shift+Enter
- Windows+R
- 输入cmd
- 按ctrl+ shift+Enter
If you don't have administrator rights on the system, go to the Chocolatey website. You may not be completely out of luck and can perform a limited local install, but I won't cover that here.
如果您没有系统管理员权限,请访问Chocolatey 网站。您可能不会完全走运并且可以执行有限的本地安装,但我不会在这里介绍。
- Copy the string below into your command prompt and type Enter:
- 将下面的字符串复制到您的命令提示符中并键入 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 will be downloaded and installed for you as below:
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
Either Exitthe CMD prompt or type the following command to reload the environment variables:
要么退出的CMD提示或键入以下命令以重新加载环境变量:
refreshenv
PowerShell Chocolatey Installation
PowerShell Chocolatey 安装
If you prefer PowerShell to the cmd prompt, you can do this directly from there, however you will have to tell PowerShell to run with a proper script execution policy to get it to work. On Windows 10, the simplest way I have found to do this is to type the following into the Cortana search bar next to the Windows button:
如果您更喜欢 PowerShell 而不是 cmd 提示符,您可以直接从那里执行此操作,但是您必须告诉 PowerShell 使用正确的脚本执行策略运行才能使其工作。在 Windows 10 上,我发现最简单的方法是在 Windows 按钮旁边的 Cortana 搜索栏中键入以下内容:
PowerShell.exe
Next, right click on the 'Best Match' choice in the menu that pops up and select 'Run as Administrator'
接下来,右键单击弹出菜单中的“最佳匹配”选项,然后选择“以管理员身份运行”
Now that you're in PowerShell, hopefully running with Administrator privileges, execute the following to install Chocolatey:
现在你在 PowerShell 中,希望以管理员权限运行,执行以下命令来安装 Chocolatey:
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
PowerShell will download Chocolatey for you and launch the installation. It only takes a few moments. It looks exactlylike the CMD installation, save perhaps some fancy colored text.
PowerShell 将为您下载 Chocolatey 并启动安装。只需要几分钟。它看起来与 CMD 安装完全一样,可能只是保留了一些花哨的彩色文本。
Either ExitPowerShell or type the following command to reload the environment variables:
无论是退出的PowerShell或键入以下命令来重新加载环境变量:
refreshenv
Upgrading Python
升级 Python
The choco command is the same whether you use PowerShell or the cmd prompt. Launch your favorite using the instructions as above. I'll use the administrator cmd prompt:
无论您使用 PowerShell 还是 cmd 提示符,choco 命令都是相同的。使用上述说明启动您最喜欢的。我将使用管理员 cmd 提示符:
C:\WINDOWS\system32>choco upgrade python -y
Essentially, chocolatey will tell you "Hey, Python isn't installed" since you're coming from 2.7.x and it treats the 2.7 version as completelyseparate. It is onlygoing to give you the most current version, 3.x.y (as of this writing, 3.7.2, but that will change in a few months):
本质上,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).
Eitherexit out of the cmd/Powershell prompt and re-enter it, or use refreshenv then type py --version
要么退出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
Note that the most recent Python install will now take over when you type Python at the command line. You can run either version by using the following commands:
请注意,当您在命令行中键入 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:\>
From here I suggest you use the Python piputility to install whatever packages you need. For example, let's say you wanted to install Flask. The commands below first upgrade pip, then install Flask
从这里我建议您使用 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
...will do the trick. Happy Pythoning!
...会做的伎俩。快乐的蟒蛇!
回答by Vlad Bezden
If you are upgrading any 3.x.y to 3.x.z (patch) Python version, just go to Python downloads pageget the latest version and start the installation. Since you already have Python installed on your machine installer will prompt you for "Upgrade Now". Click on that button and it will replace the existing version with a new one. You also will have to restart a computer after installation.
如果您要将任何 3.xy 升级到 3.xz(补丁)Python 版本,只需转到Python 下载页面获取最新版本并开始安装。由于您的机器上已经安装了 Python,安装程序会提示您“立即升级”。单击该按钮,它将用新版本替换现有版本。您还必须在安装后重新启动计算机。
If you are upgrading from 3.x to 3.y (minor) then you will be prompted with "Install Now". In this case, you are not upgrading, but you are installing a new version of Python. You can have more than one version installed on your machine. They will be located in different directories. When you have more than one Python version on your machine you will need to use py lanucherto launch a specific version of Python.
如果您要从 3.x 升级到 3.y(次要),则会提示您“立即安装”。在这种情况下,您不是在升级,而是在安装 Python 的新版本。您可以在您的机器上安装多个版本。它们将位于不同的目录中。当您的机器上有多个 Python 版本时,您将需要使用py lanucher来启动特定版本的 Python。
For instance:
例如:
py -3.7
py -3.7
or
或者
py -3.8
py -3.8
Make sure you have py launcherinstalled on your machine. It will be installed automatically if you are using default settings of windows installer. You can always check it if you click on "Customize installation" link on the installation window.
确保您的机器上安装了py 启动器。如果您使用 Windows 安装程序的默认设置,它将自动安装。如果您单击安装窗口上的“自定义安装”链接,您可以随时检查它。
If you have several Python versions installed on your machine and you have a project that is using the previous version of Python using virtual environment e.g. (venv) you can upgrade Python just in that venv using:
如果您的机器上安装了多个 Python 版本,并且您有一个使用虚拟环境(例如 (venv) 使用以前版本的 Python 的项目),您可以使用以下命令在该 venv 中升级 Python:
python -m venv --upgrade "your virtual environment path"
python -m venv --upgrade "你的虚拟环境路径"
For instance, I have Python 3.7 in my ./venv virtual environment and I would like upgrade venv to Python 3.8, I would do following
例如,我的 ./venv 虚拟环境中有 Python 3.7,我想将 venv 升级到 Python 3.8,我会执行以下操作
python -m venv --upgrade ./venv
python -m venv --upgrade ./venv
回答by Mike Poole
The easiest way of all to install the latest version of Python (if you have uninstalled other versions) is to type Python
into PowerShell.
安装最新版本 Python 的最简单方法(如果您已经卸载了其他版本)是在Python
PowerShell 中键入。
This will open the Microsoft Store and you click the Get
button.
这将打开 Microsoft Store,然后单击Get
按钮。