Sane Python environment Part 3, pipx
Managing Python-based command-line tools with pipx

pipx is a command-line tool that installs Python-based command-line tools into separated "virtual" environments, which are isolated from your global Python installation and from each other. We will cover virtual environments and the virtualenv tooling in more depth in Part 4.

Please note that pipx is not made for installing Python libraries. It is made only for installing Python command-line tools, that is packages which have command-line endpoints like flake8 or youtube-dl. You can find an interesting comparison of pipx vs other similar tools in the pipx documentation.

Note: This tutorial is part of a series about Python environments. You can go back to the first part with this link: Sane Python environment Part 1, isolation.

Pre-requisites

Before starting this tutorial, make sure you installed pyenv using Part 1 of the tutorial.

Installation

Before installing pipx, make sure your global Python points to a specific version of Python 3 such as 3.8.1, and not to the system version.

Indeed, pipx is installed with pip, which makes it dependent on your global Python installation, and you don't want to ruin your system installation. Moreover, pipx will attempt to install all tools using the version of the pyenv it was itself installed with. That is also why it's important to have picked a version of Python 3 as global Python, because nowadays most tools are no longer maintained in Python 2. If you change your global Python to another version than the one pipx was installed with, calls to pipx will be rejected by pyenv, saying that there is no pipx command for the global Python you are using.

These caveats being said, here are the commands to install pipx:

  • pip install pipx
  • pipx ensurepath which adds the pipx bin directory to your $PATH, similarly to when we modified the $PATH for pyenv

Note that in the pipx documentation, the author is recommending to add the --user flag, which makes Pip install in your $HOME instead of a sub-directory of /usr. In our case, it is not necessary because pyenv is already installed in your $HOME by design.

Usage

Once pipx is installed, you can use it to install all kinds of Python command line tools, such as flake8:

➜ pipx install flake8
Cache entry deserialization failed, entry ignored
  installed package flake8 3.7.9, Python 3.8.1
  These apps are now globally available
    - flake8
done! ✨ 🌟 ✨

# See how flake8 was installed in a virtualenv by pipx
➜ ls -al $(which flake8)
[...] /home/pierre/.local/bin/flake8 -> /home/pierre/.local/pipx/venvs/flake8/bin/flake8

As shown by the ls command, pipx installs all tools within their own separate directory inside the ~/.local/pipx/venvs directory and symlinks them to ~/.local/bin.

Thanks to these isolated directories in ~/.local/pipx/venv, we are no longer worried that a new tool might break an existing tool. Indeed, each tool upgrade or installation occurs within its own directory without any possible side-effect on other tools. Whenever you want to upgrade your tools, you can simply run pipx upgrade-all. You can also use pipx upgrade to selectively upgrade some of your tools only.

Uninstallation

If you need to uninstall pipx, first remove all the tools it installed:

  • pipx uninstall-all

Then, similarly to pyenv:

  1. in your Shell config, remove the export PATH=... line containing pipx
  2. remove the pipx directory:
    • rm -rf ~/.local/pipx
  3. exit and re-open your terminal

Now that you are familiar with pipx, you can learn about Python virtualenvs in Part 4 of the tutorial.