translated

This commit is contained in:
chai001125 2022-11-05 15:53:52 +08:00
parent 8cca42257e
commit 6dd55bc5a2
2 changed files with 108 additions and 108 deletions

View File

@ -1,108 +0,0 @@
[#]: subject: "How to Upgrade Python Packages with Pip"
[#]: via: "https://itsfoss.com/upgrade-pip-packages/"
[#]: author: "Sagar Sharma https://itsfoss.com/author/sagar/"
[#]: collector: "lkxed"
[#]: translator: "chai001125"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
How to Upgrade Python Packages with Pip
======
When was the last that you updated Python packages installed via Pip? Most of the users tend to forget that those packages also need to be updated, as just updating the system repository is not going to work here.
So lets take a moment and see how to update old Python packages with Pip.
### How to use pip to upgrade Python packages
[Pip (Pip Installs Packages)][1] is a command line utility to manage python packages. You can think of this as how we use apt to manage packages in Ubuntu and Debian.
So lets dive deep into how you can use this fab utility to manage everything related to Python packages.
#### 1. List outdated packages
Listing the outdated packages is the best idea to plan how you want to update packages as not many want to update their entire library of packages at once and wants to be selective.
To list outdated packages of Python, you just have to pair `pip` command with `list` option and `--outdated` flag as shown:
```
pip list --outdated
```
![outdated packages][2]
#### 2. Upgrade a specific package
Once you get the list of the packages that need to be updated, you can be selective as I mentioned earlier, and to update a specific package, youll need to follow the given command syntax:
```
pip install package_name -U
```
For example, I want to upgrade the package named `anime-api` to the most recent version, so Ill be using the given command:
```
pip install anime-api -U
```
![update anime api][3]
#### 3. Upgrade package to specific version
It is not necessary to use only the most recent version of the software (cough [Debian][4] cough) and if you are in need of using packages to a specific version that may or may not be the most recent software, can be done using the given command syntax:
```
pip install --upgrade <package>==<version>
```
So I want to update the package named `xdg` to version 5.1 which is one point release behind the most recent build so my command would be:
```
pip install --upgrade xdg==5.1
```
![upgrade xdg to specific iteration][5]
#### 4. Upgrade every package using Pip
**NOTE: I do not recommend upgrading every package at once as most of the time, the dependencies are too complex to be handled.**
To upgrade every python package, youd need to follow the given command:
```
pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3 install -U
```
![upgrade everything][6]
The above command utilizes [xargs][7]. First, it will grab the packages that are needed to be updated and then perform `pip3 install -U` command over each package.
And I used pip3 here instead of pip. In Ubuntu 22.04 and later, both pip and pip3 commands are available.
### Wrapping Up
Upgrading everything at once has never been a good idea in the case of pip. And I found myself in a state of broken dependencies so make sure you know what you will have.
And if you have any queries, feel free to ask in the comments.
--------------------------------------------------------------------------------
via: https://itsfoss.com/upgrade-pip-packages/
作者:[Sagar Sharma][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/sagar/
[b]: https://github.com/lkxed
[1]: https://itsfoss.com/install-pip-ubuntu/
[2]: https://itsfoss.com/wp-content/uploads/2022/09/outdated-packages.png
[3]: https://itsfoss.com/wp-content/uploads/2022/09/update-anime-api.png
[4]: https://www.debian.org/
[5]: https://itsfoss.com/wp-content/uploads/2022/09/upgrade-xdg-to-specific-iteration.png
[6]: https://itsfoss.com/wp-content/uploads/2022/09/upgrade-everything.png
[7]: https://linuxhandbook.com/xargs-command/

View File

@ -0,0 +1,108 @@
[#]: subject: "How to Upgrade Python Packages with Pip"
[#]: via: "https://itsfoss.com/upgrade-pip-packages/"
[#]: author: "Sagar Sharma https://itsfoss.com/author/sagar/"
[#]: collector: "lkxed"
[#]: translator: "chai001125"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
使用 Pip 升级 Python 软件包
======
你上次更新通过 Pip 安装的 Python 软件包是什么时候?大多数用户往往会忘记这些 Python 软件包也需要手动更新,因为仅仅更新系统存储库对于软件包来说是不起作用的。
因此,让我们花点时间看看如何使用 Pip来更新旧的 Python 软件包吧。
### 如何使用 Pip 升级 Python 软件包
[PipPip Installs Packages][1] 是一个用于管理 Python 软件包的 <ruby>命令行实用程序 <rt> command line utility </rt></ruby> 。你可以将 Pip 安装 Python 软件包,类比为在 Ubuntu 和 Debian 中使用 `apt` 管理软件包那样。
因此,接下来就让我们深入了解如何使用这个极好的工具 Pip来管理与 Python 软件包相关的内容吧。
#### 1. 列出过时的 Python 软件包
在计划更新什么软件包之前,我们先要列出有哪些过时的软件包,你可以在其中选择想要更新的软件包,因为大多数人不会想一下子更新整个软件包库。
要列出过时的 Python 软件包,你只需将 `pip` 命令与 `list` 选项、`--outdated` 标志一同使用即可,如下图所示:
```
pip list --outdated
```
![outdated packages][2]
#### 2. 升级特定的软件包
获得可更新的软件包列表后你可以像我之前提到的那样选择你要更新的那个特定的软件包pip 升级软件包命令的语法如下:
```
pip install package_name -U
```
例如,我想将名为 `anime-api` 的软件包升级到最新版本,所以我将使用下面的命令来升级:
```
pip install anime-api -U
```
![update anime api][3]
#### 3. 将软件包升级到特定的版本
没有必要总是使用软件的最新版本,如果你想将软件包升级到不是最新的某个特定版本,参考如下的命令语法:
```
pip install --upgrade <package>==<version>
```
例如,我想将名为 `xdg` 的软件包更新到 5.1 版本5.1 版本是最新版本的前一个版本,所以可以使用以下命令:
```
pip install --upgrade xdg==5.1
```
![upgrade xdg to specific iteration][5]
#### 4. 使用 Pip 一次性升级所有软件包
**请注意:我不建议你一次性升级所以软件包,因为 Python 软件包的依赖项太复杂了,一次性的升级无法处理相互依赖项。**
要一次性升级所有 python 软件包,你可以使用以下命令:
```
pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3 install -U
```
![upgrade everything][6]
上面的命令使用了 [xargs][7]。首先,会得到所有需要更新的软件包,然后对每个软件包执行 `pip3 install -U` 命令。
我在这里使用的是 pip3而不是 pip。在 Ubuntu 22.04 及更高的版本中pip 和 pip3 命令都可以使用。
### 总结
使用 Pip 一次性更新所有 Python 软件包并不是一个好主意。我发现一次性更新后,软件包之间的依赖关系被破坏了,所以请确保只更新你想要更新的软件包。
如果你还有其他的疑问,就请在评论区中留言吧。
--------------------------------------------------------------------------------
via: https://itsfoss.com/upgrade-pip-packages/
作者:[Sagar Sharma][a]
选题:[lkxed][b]
译者:[chai001125](https://github.com/chai001125)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/sagar/
[b]: https://github.com/lkxed
[1]: https://itsfoss.com/install-pip-ubuntu/
[2]: https://itsfoss.com/wp-content/uploads/2022/09/outdated-packages.png
[3]: https://itsfoss.com/wp-content/uploads/2022/09/update-anime-api.png
[4]: https://www.debian.org/
[5]: https://itsfoss.com/wp-content/uploads/2022/09/upgrade-xdg-to-specific-iteration.png
[6]: https://itsfoss.com/wp-content/uploads/2022/09/upgrade-everything.png
[7]: https://linuxhandbook.com/xargs-command/