pipx: Install and Run Python Applications in Isolated Environments

1.11.1 · active · verified Thu Apr 09

pipx (Python Package Installer for Executables) is a tool to install and run Python applications in isolated environments. It ensures that executable applications installed via pip do not interfere with your system's global Python environment or other projects. It's currently at version 1.11.1 and follows an active, irregular release cadence, often aligning with important bug fixes or Python version support changes. pipx is primarily a command-line interface tool.

Warnings

Install

Quickstart

This quickstart demonstrates the core workflow of pipx: installing pipx itself, then using it to install a sample CLI application (cowsay), running it, listing installed applications, and finally uninstalling it. All interactions are done by calling pipx as a command-line tool via subprocess, reflecting its primary mode of use.

import subprocess
import sys
import os

# This quickstart demonstrates installing an application via pipx.
# pipx is primarily a command-line tool, so interaction is via subprocess calls.

# 1. Ensure pipx itself is installed
print("Ensuring pipx is installed...")
subprocess.run([sys.executable, "-m", "pip", "install", "pipx"], check=True, capture_output=True)
print("pipx ensured to be installed.")

# 2. Install a sample application (e.g., 'cowsay')
app_name = "cowsay"
print(f"\nAttempting to install '{app_name}' with pipx...")
try:
    subprocess.run(["pipx", "install", app_name], check=True, capture_output=True)
    print(f"'{app_name}' installed successfully.")
except subprocess.CalledProcessError as e:
    if "is already installed" in e.stderr.decode():
        print(f"'{app_name}' was already installed. Proceeding.")
    else:
        print(f"Error installing {app_name}: {e.stderr.decode()}")
        sys.exit(1)

# 3. Run the installed application
message = "Hello from checklist.day's pipx quickstart!"
print(f"\nRunning '{app_name}' via pipx...")
try:
    # Use 'pipx run' for demonstration, which temporarily adds to PATH
    # and executes. For persistently installed apps, users would typically
    # run 'cowsay <message>' directly after 'pipx ensurepath'.
    result = subprocess.run(["pipx", "run", app_name, message], capture_output=True, text=True, check=True)
    print(result.stdout)
except subprocess.CalledProcessError as e:
    print(f"Error running {app_name}: {e.stderr.decode()}")
    sys.exit(1)

# 4. List installed applications and uninstall
print("\nListing pipx installed applications:")
subprocess.run(["pipx", "list"], check=True)

print(f"\nUninstalling '{app_name}'...")
subprocess.run(["pipx", "uninstall", app_name], check=True, capture_output=True)
print(f"'{app_name}' uninstalled successfully.")

view raw JSON →