How to install Python applications in isolated environments with pipx

If you are familiar with Python, you surely used pip: the Python package installer. With pip, we can install packages “globally” or in virtual environments. Virtual environments are mainly used to install dependencies of specific projects, so to develop them in isolation. We usually install packages, “globally”, instead, when want to access the utilities they provide from anywhere. By using pipx we can get the best of both approaches: we can install each application in its own virtual environment, and, at the same time, access it globally.

In this tutorial we see how to install pipx on some of the most used Linux distributions, and how to use it to install and execute Python applications in isolated environments.

In this tutorial you will learn:

  • How to install pipx on the most common Linux distributions
  • How to use pipx to install and execute Python applications in isolated environments
  • How to run applications temporarily
  • How to upgrade, list and remove packages
How to install python applications in isolated environments with pipx
How to install python applications in isolated environments with pipx – original image by starline on Freepik
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Distribution agnostic
Software pipx
Other Privileged access to your Linux system as root or via the sudo command in order to perform system-wide installation of required packages
Conventions # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux-commands to be executed as a regular non-privileged user

Installation

Pipx is actively developed on Github, and is packaged and available in the official repositories of all the most used Linux distributions. To install pipx on Fedora, we can run:

$ sudo dnf install pipx



On Debian and Debian-based distributions, instead, we can perform the installation by using apt:

$ sudo apt install pipx

On Archlinux, we can install the package using pacman:

$ sudo pacman -S python-pipx

Being pipx itself a Python package, it can also be installed with pip:

$ pip install pipx --user

Why pipx?

Why should one use pipx? What problem does it solve? By using pipx we gain the ability to automatically install packages in their own virtual environments, and at the same time launch the utilities they provide from anywhere in the system. Pipx is specifically designed to install packages which expose one or more command line or graphical utilities; it even allows us to run applications “temporarily”, keeping them on the system just until they exit.

Installing applications with pipx

If a package doesn’t contain any executable, pipx will refuse to install it. Suppose, for example, we want to scrape a web page using the BeautifulSoup library. If we attempt to install the “bs4” package, pipx responds this way:

$ pipx install bs4

No apps associated with package bs4 or its dependencies. If you are attempting  to 
install a library, pipx should not be used. Consider using pip or a similar tool instead.



The message its pretty clear, isn’t? Let’s try again. This time we will try to install  the “ansible-core” package, which contains Ansible core utilities such as ansible-playbook, ansible-console, and ansible-vault:

$ pipx install ansible-core
  installed package ansible-core 2.15.2, installed using Python 3.11.4
  These apps are now globally available
    - ansible
    - ansible-config
    - ansible-connection
    - ansible-console
    - ansible-doc
    - ansible-galaxy
    - ansible-inventory
    - ansible-playbook
    - ansible-pull
    - ansible-test
    - ansible-vault
done! ✨ 🌟 ✨

As expected, this time everything worked correctly. As stated by the message, the utilities contained in the package are now globally available. Where did pipx install the package? Pipx installs packages, together with their dependencies, in their own virtual environments, under the ~/.local/pipx/venvs directory. In our case, we can see only the ansible-core environment exists:

$ ls -l ~/.local/pipx/venvs
total 4 
drwxr-xr-x. 5 doc doc 4096 Jul 31 16:10 ansible-core

To make the utilities exposed by a package globally accessible, links are created in the ~/.local/bin directory. Indeed, we can see how, after installing the “ansible-core” package, the ~/.local/bin directory contains symbolic links pointing to executables in the virtual environment:

$ ls ~/.local/bin -l 
total 16 
lrwxrwxrwx. 1 doc doc 52 Jul 31 16:10 ansible -> /home/doc/.local/pipx/venvs/ansible-core/bin/ansible 
lrwxrwxrwx. 1 doc doc 59 Jul 31 16:10 ansible-config -> /home/doc/.local/pipx/venvs/ansible-core/bin/ansible-config 
lrwxrwxrwx. 1 doc doc 63 Jul 31 16:10 ansible-connection -> /home/doc/.local/pipx/venvs/ansible-core/bin/ansible-connection 
lrwxrwxrwx. 1 doc doc 60 Jul 31 16:10 ansible-console -> /home/doc/.local/pipx/venvs/ansible-core/bin/ansible-console 
lrwxrwxrwx. 1 doc doc 56 Jul 31 16:10 ansible-doc -> /home/doc/.local/pipx/venvs/ansible-core/bin/ansible-doc 
lrwxrwxrwx. 1 doc doc 59 Jul 31 16:10 ansible-galaxy -> /home/doc/.local/pipx/venvs/ansible-core/bin/ansible-galaxy 
lrwxrwxrwx. 1 doc doc 62 Jul 31 16:10 ansible-inventory -> /home/doc/.local/pipx/venvs/ansible-core/bin/ansible-inventory 
lrwxrwxrwx. 1 doc doc 61 Jul 31 16:10 ansible-playbook -> /home/doc/.local/pipx/venvs/ansible-core/bin/ansible-playbook 
lrwxrwxrwx. 1 doc doc 57 Jul 31 16:10 ansible-pull -> /home/doc/.local/pipx/venvs/ansible-core/bin/ansible-pull 
lrwxrwxrwx. 1 doc doc 57 Jul 31 16:10 ansible-test -> /home/doc/.local/pipx/venvs/ansible-core/bin/ansible-test 
lrwxrwxrwx. 1 doc doc 58 Jul 31 16:10 ansible-vault -> /home/doc/.local/pipx/venvs/ansible-core/bin/ansible-vault

Observe how didn’t provide a --user flag to specify we want to install packages for a specific user: this is pipx default behavior.

Running an application temporary

With pipx we have the ability to run an application temporarily: by using the run subcommand, the corresponding package is installed in a temporary virtual environment; the application is then launched, and automatically removed as soon as it exits. When the utility we want to run has the same name of the package which provides it, we can simply run:

$ pipx run <utility-name>



When a package contains multiple executables, instead, as in the case of the “ansible-core” package we installed in the previous example, we need to use the --spec option to specify the package name, and than specify the command we want to run. Suppose we want to use “ansible” and the “gather_facts” module to retrieve information about localhost. We would run:

$ pipx run --spec ansible-core ansible -m gather_facts localhost

Listing installed packages

In order to list the packages installed via pipx, we can use the “list” subcommand:

$ pipx list
venvs are in /home/doc/.local/pipx/venvs 
apps are exposed on your $PATH at /home/doc/.local/bin 
  package ansible-core 2.15.2, installed using Python 3.11.4 
   - ansible 
   - ansible-config 
   - ansible-connection 
   - ansible-console 
   - ansible-doc 
   - ansible-galaxy 
   - ansible-inventory 
   - ansible-playbook 
   - ansible-pull 
   - ansible-test 
   - ansible-vault

As you can see, the output of the command reports where virtual environments are installed and where utilities are linked.

Upgrading packages

When using pipx we have the chance to upgrade a single package or all packages at once. In our case, to update only the “ansible-core” package, we would run:

$ pipx upgrade ansible-core

To upgrade all installed package, instead:

$ pipx upgrade-all

Removing packages

Since each package is installed in its own virtual environment, it can be easily removed from the system in a clean way, together with all its dependencies. The command which let us accomplish this task is: “uninstall”. In our case, to uninstall the “ansible-core” package from our system, we would run:

$ pipx uninstall picard

Conclusions

In this tutorial we learned how to install and execute Python applications in isolated environments using pipx. Pipx is designed to install only Python packages which expose one or more executables. Packages and their dependencies are installed in their own virtual environments; links to the utilities they provide are then created inside a directory in the user PATH, so they can be executed globally.



Comments and Discussions
Linux Forum