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

Mos*_* S. 68 python python-3.x

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

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

pok*_*oke 67

每个次要版本的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 pip运行pip.因此,例如,为Python 3.6安装Beautiful Soup,您可以运行py -3.6 -m pip install beautifulsoup4.

  • **注意:** `py` 是 [Windows 版 Python 启动器](https://docs.python.org/3/using/windows.html#launcher)。或者参见定义它的 [PEP 397](https://www.python.org/dev/peps/pep-0397/)。没有为 Linux/Mac 安装它。 (2认同)

gga*_*epy 13

Installing/Upgrading Python Using the Chocolatey Windows Package Manager

Let's say you have Python 2.7.16:

    C:\Windows\system32>python --version
    python2 2.7.16
Run Code Online (Sandbox Code Playgroud)

...and you want to upgrade to the (now current) 3.x.y version. There is a simple way to install a parallel installation of Python 3.x.y using a Windows package management tool.

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.

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.

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

Installing Chocolatey

To get the Chocolatey Package Manager, you follow a process that is described at 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 Prompt Chocolatey Installation

Launch an administrative command prompt. On Windows 10, to do this:

  • Windows+R
  • Type cmd
  • Press 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.

  • Copy the string below into your command prompt and type 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"
Run Code Online (Sandbox Code Playgroud)

Chocolatey will be downloaded and installed for you as below:

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
Run Code Online (Sandbox Code Playgroud)

Either Exit the CMD prompt or type the following command to reload the environment variables:

refreshenv
Run Code Online (Sandbox Code Playgroud)

PowerShell Chocolatey Installation

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.exe
Run Code Online (Sandbox Code Playgroud)

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:

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

PowerShell will download Chocolatey for you and launch the installation. It only takes a few moments. It looks exactly like the CMD installation, save perhaps some fancy colored text.

Either Exit PowerShell or type the following command to reload the environment variables:

refreshenv
Run Code Online (Sandbox Code Playgroud)

Upgrading 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:

C:\WINDOWS\system32>choco upgrade python -y
Run Code Online (Sandbox Code Playgroud)

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 completely separate. It is only going 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 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).
Run Code Online (Sandbox Code Playgroud)

Either exit out of the cmd/Powershell prompt and re-enter it, or use refreshenv then type 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
Run Code Online (Sandbox Code Playgroud)

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:

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:\>
Run Code Online (Sandbox Code Playgroud)

From here I suggest you use the Python pip utility 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

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
Run Code Online (Sandbox Code Playgroud)

...will do the trick. Happy Pythoning!


Vla*_*den 7

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

在此处输入图片说明

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

例如:

py -3.7

要么

py -3.8

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

  • 在Windows 10上从3.7.2迁移到3.7.4(64位),我看不到,我看到“立即安装”,在这里看到“立即升级” (4认同)
  • 我刚刚从 3.7.3 升级到 3.7.5,并收到“立即升级”消息。如果我尝试从 3.7.3 升级到 3.8,则会看到“立即安装”。 (3认同)
  • @Suncatcher,你是对的,这就是我所描述的“如果你想从 3.x 升级到 3.y 那么......”部分。请阅读以上内容。 (3认同)
  • @MosheS.you 可以使用以下命令获取过时的软件包列表: pip list --outdated ,然后通过 python -m pip install --upgrade <your package> 更新每个软件包 (2认同)

jAC*_*jAC 6

对我来说,一种快速且无痛的方法是执行以下操作:

  1. 对我受影响的环境进行操作pip freeze > requirements.txt(或者您想要备份您的需求的任何方法)
  2. 删除旧版本的 Python(在我的例子中是 3.8)
  3. 删除关联环境
  4. 安装新版本(我的例子是3.9.5)
  5. 重新创建我的环境python -m venv venv或按照您的意愿重新创建
  6. 重新安装我的插件/应用程序pip install -r requirements.txt或按照您的意愿重新安装


ros*_*e_x 5

2019 年,您可以使用Chocolatey进行安装。打开 cmd 或 powershell,输入:

choco install python
Run Code Online (Sandbox Code Playgroud)


小智 5

现在,您只需从 Microsoft Store 安装多个 Python 版本即可。这些由 Python Software Foundation 在那里发布。

Microsoft Store 提供 Python 版本

https://apps.microsoft.com/store/search?hl=en-en&gl=EN&publisher=Python%20Software%20Foundation