在 Linux 中安装和使用 pipx
pipx pip
![](/data/attachment/album/202306/17/151434qbybncmn7yakkewb.jpg)



> 
> `pipx` 解决了流行的 `pip` 工具的不足之处。学习在 Linux 中安装和使用 `pipx`。
> 
> 
> 


`pip` 是一种流行的工具，用于从 [Python 包索引](https://pypi.org:443/) 中安装 Python 包和模块。


然而，在最近的发行版本中，`pip` 用户遇到了 [外部管理环境错误](https://itsfoss.com/externally-managed-environment/)。


![error: externally-managed-environment](/data/attachment/album/202306/17/151510wh9mqqy9z9qymy9h.png)


这是为避免通过 [pip](https://itsfoss.com/install-pip-ubuntu/) 安装的 Python 包与本机包管理器之间发生冲突而添加的“功能”。Python 希望你使用单独的虚拟环境，而不是通过 `pip` 在全局安装包。


**这就是 `pipx` 发挥作用的地方**。它为你安装的每个应用创建一个新的虚拟环境，然后在全局级别的 `/bin` 中创建到本地二进制文件的链接。这一切都是自动的。它为你节省时间和精力。


让我们看看如何在 Ubuntu 和其他 Linux 发行版上安装和使用 `pipx`。


### 在 Ubuntu 和其他 Linux 上安装 pipx


安装很简单，可以在 Ubuntu 和 Debian 上使用以下命令安装：



```
sudo apt update && sudo apt install pipx

```

对于其他发行版，请使用你的包管理器并安装它。


完成安装后，[将其添加到 $PATH](https://itsfoss.com/add-directory-to-path-linux/) 以便可以从任何地方访问它：



```
pipx ensurepath

```

![install pipx in ubuntu](/data/attachment/album/202306/17/151511rgrgcqp11qrgqlqt.png)


**关闭终端并重新启动它**。这就完成了！ 现在，让我们看看如何使用它。


### 使用 pipx


包管理器的主要用途是什么？ 软件包安装、更新和删除。


让我向你展示如何使用 `pipx` 执行以下操作：


* 搜索包
* 包安装
* 升级
* 包移除


让我们从安装开始。


#### 如何使用 pipx 安装包


要使用 `pipx` 安装包，你必须遵循一个简单的命令语法：



```
pipx install <package_name>

```

例如，在这里，我安装了一个非常有用的程序 Cowsay：



```
pipx install cowsay

```

![install python packages in isolation using pipx in ubuntu](/data/attachment/album/202306/17/151511f99wrri9iuhirw9p.png)


同样，如果你想安装特定版本的包，你必须在 `==` 后跟上版本号，如下所示：



```
pipx install package==version

```

例如，在这里，我安装了 1.24.1 版本的 numpy：



```
pipx install numpy==1.24.1

```

![](/data/attachment/album/202306/17/151511na8c6u74y9hh7ehb.png)


#### 如何搜索包


`pipx` 程序没有搜索功能（因为 PyPI 的 API 使用限制）但这并不意味着你不能搜索 Python 包。


要搜索包，你需要安装 `pypisearch`：



```
pipx install pypisearch

```

完成后，你可以使用 `pypisearch` 命令搜索包：



```
pypisearch python_package_name

```

在这里，我搜索了 neofetch：


![search python packages in Ubuntu](/data/attachment/album/202306/17/151512ovrlpilgvod9vv6v.png)


#### 如何使用 pipx 升级包


与任何其他现代包管理器一样，你可以一次升级所有包，也可以一次升级一个包。


要一次升级所有软件包，你只需执行以下命令：



```
pipx upgrade-all

```

![upgrade all the python packages at once in ubuntu](/data/attachment/album/202306/17/151512xfk2zf225kjdlz2d.png)


如你所见，它将 numpy 升级到最新版本。


但是如果你想升级一个特定的包，你可以这样做：



```
pipx upgrade package-name

```

假设我想将 `cowsay` 包升级到最新版本，那么，我将使用以下命令：



```
pipx upgrade cowsay

```

![upgrade specific python package using pipx in ubuntu](/data/attachment/album/202306/17/151512c364bt46cnx3m6b6.png)


#### 如何使用 pipx 卸载包


要删除包，你必须使用 `uninstall` 标志，如下所示：



```
pipx uninstall package_name

```

为了供你参考，在这里，我从我的系统中删除了 `numpy`：



```
pipx uninstall numpy

```

![remove python packages using pipx in ubuntu](/data/attachment/album/202306/17/151512gzovkc6v08ohdok2.png)


### pip 还是 pipx？


对 `pip` 的限制影响了最终用户对它的使用。值得庆幸的是，`pipx` 提供了急需的替代方案。它符合使用虚拟环境的 Python 准则，同时允许已安装的应用在全局范围内可用。


对于不是 Python 应用开发的最终用户，这提供了使用发行版仓库中不可用的 Python 应用的选项。


希望本教程对你有所帮助。如果你有任何问题或建议，请告诉我。


*（题图：MJ/fc6190e2-a412-443e-a83c-91fce1dcf695）*




---


via: <https://itsfoss.com/install-pipx-ubuntu/>


作者：[Sagar Sharma](https://itsfoss.com/author/sagar/) 选题：[lkxed](https://github.com/lkxed/) 译者：[geekpi](https://github.com/geekpi) 校对：[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)

Pip is a popular tool for installing Python packages and modules from [Python Package Index](https://pypi.org/).

However, in recent distribution versions, pip users are encountering an [externally-managed-environment error](https://itsfoss.com/externally-managed-environment/).

![error: externally-managed-environment](https://itsfoss.com/content/images/2023/05/error--externally-managed-environment.png)

That's a 'feature' added to avoid conflicts between Python packages installed via [Pip](https://itsfoss.com/install-pip-ubuntu/) and the native package manager. Python wants you to use separate virtual environments instead of installing the package at the global level via Pip.

**This is where pipx comes into the picture**. It creates a new virtual environment for each application you install and then creates links to local binary in the /bin at the global level. All this is automatic. It saves time and effort for you.

Let's see how to install and use Pipx on Ubuntu and other Linux distributions.

## Install pipx on Ubuntu and other Linux

The installation is straightforward and can be installed using the following command on Ubuntu and Debian:

`sudo apt update && sudo apt install pipx`


For other distributions, please use your package manager and install it.

Once you are done with the installation, [add it to the $PATH](https://itsfoss.com/add-directory-to-path-linux/) so it can be accessed from everywhere:

`pipx ensurepath`


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

**Close the terminal and start it again**. That's it! Now, let's have a look at how to use it.

## Using pipx

What is the primary use of a package manager? Package installation, updation, and removal.

Let me show how you can do the following with pipx:

- Search packages
- Package installation
- Upgradation
- Package removal

Let's start with the installation.

### How to install packages using pipx

To install packages using pipx, you'd have to follow a simple command syntax:

`pipx install <package_name>`


For example, here, I installed a very useful program Cowsay:

`pipx install cowsay`


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

Similarly, if you want to install a specific version of the package, you'd have to insert the version number followed by `==`

as shown:

`pipx install package==version`


For example, here, I installed numpy version 1.24.1:

`pipx install numpy==1.24.1`


![](https://itsfoss.com/content/images/2023/05/install-specific-version-of-python-package-in-ubuntu-using-pipx.png)

### How to search packages

The pipx utility does not have a search feature (because of limited API use of PyPI) but that doesn't mean you can't search Python packages.

To search packages, you'd have to install `pypisearch`

:

`pipx install pypisearch`


Once you do that, you can search the packages using the `pypisearch`

command:

`pypisearch python_package_name`


Here, I searched for neofetch:

![search python packages in Ubuntu](https://itsfoss.com/content/images/2023/05/search-python-packages-in-Ubuntu.png)

### How to upgrade packages using pipx

Like any other modern package manager, you can upgrade all packages at once or you can upgrade one package at a time.

To upgrade all the packages at once, all you have to do is execute the following command:

`pipx upgrade-all`


![upgrade all the python packages at once in ubuntu](https://itsfoss.com/content/images/2023/05/upgrade-all-the-python-packages-at-once-in-ubuntu.png)

As you can see, it upgraded numpy to the latest version.

But if you want to upgrade a specific package, here's how you do it:

`pipx upgrade package-name`


Let's say I want to upgrade `cowsay`

package to the latest version, then, I will be using the following:

`pipx upgrade cowsay`


![upgrade specific python package using pipx in ubuntu](https://itsfoss.com/content/images/2023/05/upgrade-specific-python-package-using-pipx-in-ubuntu.png)

### How to uninstall packages using pipx

To remove packages, you'd have to use the `uninstall`

flag as shown:

`pipx uninstall package_name`


For your reference, here, I removed `numpy`

from my system:

`pipx uninstall numpy`


![remove python packages using pipx in ubuntu](https://itsfoss.com/content/images/2023/05/remove-python-packages-using-pipx-in-ubuntu.png)

## Pip or Pipx?

The restrictions put on Pip have limited its use by the end users. Thankfully, Pipx provides the much-needed alternative. It meets the Python guidelines of using virtual environments and, at the same time, allows installed applications to be available at the global level.

For end users, who are not Python application developers, this gives the option to use Python applications unavailable in distribution repositories.

I hope you find this tutorial helpful. Let me know if you have questions or suggestions.