使用 Pip 升级 Python 软件包
Python Pip
![](/data/attachment/album/202211/07/153408lqflbw3mwxja3qm4.jpg)


你上次更新通过 Pip 安装的 Python 软件包是什么时候？大多数用户往往会忘记这些 Python 软件包也需要手动更新，因为仅仅更新系统存储库对于软件包来说是不起作用的。


因此，让我们花点时间看看如何使用 Pip，来更新旧的 Python 软件包吧。


### 如何使用 Pip 升级 Python 软件包


[Pip（Pip Installs Packages）](https://itsfoss.com/install-pip-ubuntu/) 是一个用于管理 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](/data/attachment/album/202211/07/153410v48318bnb0z4qxap.png)


#### 2、升级特定的软件包


获得可更新的软件包列表后，你可以像我之前提到的那样，选择你要更新的那个特定的软件包，pip 升级软件包命令的语法如下：



```
pip install package_name -U

```

例如，我想将名为 `anime-api` 的软件包升级到最新版本，所以我将使用下面的命令来升级：



```
pip install anime-api -U

```

![update anime api](/data/attachment/album/202211/07/153411w56fs9s0rslzsf63.png)


#### 3、将软件包升级到特定的版本


没有必要总是使用软件的最新版本，如果你想将软件包升级到不是最新的某个特定版本，参考如下的命令语法：



```
pip install --upgrade <package>==<version>

```

例如，我想将名为 `xdg` 的软件包更新到 5.1 版本，5.1 版本是最新版本的前一个版本，所以可以使用以下命令：



```
pip install --upgrade xdg==5.1

```

![upgrade xdg to specific iteration](/data/attachment/album/202211/07/153411ioeej6hj6k6dhvko.png)


#### 4、使用 Pip 一次性升级所有软件包


**请注意：我不建议你一次性升级所以软件包，因为 Python 软件包的依赖项太复杂了，一次性的升级无法处理相互依赖项。**


要一次性升级所有 python 软件包，你可以使用以下命令：



```
pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3 install -U

```

![upgrade everything](/data/attachment/album/202211/07/153412m71rz1246h22xavc.png)


上面的命令使用了 [xargs](https://linuxhandbook.com/xargs-command/)。首先，会得到所有需要更新的软件包，然后对每个软件包执行 `pip3 install -U` 命令。


我在这里使用的是 `pip3`，而不是 `pip`。在 Ubuntu 22.04 及更高的版本中，`pip` 和 `pip3` 命令都可以使用。


### 总结


使用 Pip 一次性更新所有 Python 软件包并不是一个好主意。我发现一次性更新后，软件包之间的依赖关系被破坏了，所以请确保只更新你想要更新的软件包。


如果你还有其他的疑问，就请在评论区中留言吧。




---


via: <https://itsfoss.com/upgrade-pip-packages/>


作者：[Sagar Sharma](https://itsfoss.com/author/sagar/) 选题：[lkxed](https://github.com/lkxed) 译者：[chai001125](https://github.com/chai001125) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

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 let’s 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)](https://itsfoss.com/install-pip-ubuntu/) 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 let’s 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](https://itsfoss.com/content/images/wordpress/2022/09/outdated-packages.png)

### 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, you’ll 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 I’ll be using the given command:

`pip install anime-api -U`


![update anime api](https://itsfoss.com/content/images/wordpress/2022/09/update-anime-api.png)

### 3. Upgrade package to specific version

It is not necessary to use only the most recent version of the software (cough [Debian](https://www.debian.org/?ref=itsfoss.com) 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](https://itsfoss.com/content/images/wordpress/2022/09/upgrade-xdg-to-specific-iteration.png)

### 4. Upgrade every package using Pip


*While this is doable in theory,**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, you’d 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](https://itsfoss.com/content/images/wordpress/2022/09/upgrade-everything.png)

The above command utilizes [xargs](https://linuxhandbook.com/xargs-command/?ref=itsfoss.com). 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.

Considering that [Ubuntu is deprecating the use of pip](https://itsfoss.com/externally-managed-environment/), it's better to start moving to Pipx instead.

[Install and Use pipx in Ubuntu & Other LinuxPipx addresses the shortcomings of the popular pip tool. Learn to install and use Pipx in Linux.](https://itsfoss.com/install-pipx-ubuntu/)![](https://itsfoss.com/content/images/2023/05/install-pipx-ubuntu.png)


![](https://itsfoss.com/content/images/2023/05/install-pipx-ubuntu.png)

And if you have any queries, feel free to ask in the comments.