Installing and using pipx for command-line Python tools
June 3, 2020 ยท View on GitHub
pipx is a utility for installing and running Python applications in isolated environments. This means it allows you to install command-line tools like tox in their own virtualenv that pipx manages for you using simple, pip-like commands.
Installing pipx
To install pipx on macOS, use Homebrew:
brew update
brew install pipx
If you've already installed it, it couldn't hurt to update it:
brew update
brew upgrade pipx
Configuring pipx
pipx comes with a little helper to add the things that pipx installs to your path, so you can run them:
pipx ensurepath
The only catch with that command is that it adds the pipx binary path after your existing path;
as a result, the Docker Desktop version of Docker Compose will take precedence.
So, we need to modify what it added.
Open your ~/.bashrc or ~/.zshrc file and look toward the bottom for the following line:
export PATH="$PATH:/Users/<username>/.local/bin"
We're going to reverse the order of that so ~/.local/bin comes before the existing $PATH.
Update it to look like so (remembering to replace <username> if you copy and paste from here):
export PATH="/Users/<username>/.local/bin:$PATH"
You might also want to take this opportunity to move it to wherever you have other path modifications.
pipx also has a helper that provides shell completion instructions:
pipx completions
Now open a new shell, or source whatever file(s) you just modified in your current shell.
Installing command-line tools with pipx
You can now use pipx to install packages that provide command-line tools just like pip. To install a package from PyPI:
pipx install PACKAGE
For example, to install tox:
pipx install tox
You can also inject another Python package into pipx's isolated environment for a package. For example, to install the tox plugin tox-pip-extensions:
pipx inject tox tox-pip-extensions
Further reading
- pipx documentation
- pipx examples, including options for particular versions of Python, and more
- How pipx works