Global Installation Using Pipx

pipx installs and runs Python CLI tools in isolated environments, ensuring version conflicts don't break system packages.

Why Use Pipx?

  • Isolation: Each tool runs in its own virtualenv (no pip install --user clutter).
  • Version Control: Easily switch between tool versions (e.g., poetry 1.2.3 → 1.3.0).
  • Security: Avoids polluting the global Python environment.

Installation

  1. Install pipx (system-wide):

    # macOS/Linux (via Homebrew)
    brew install pipx
    # OR via pip (if not using Homebrew)
    python3 -m pip install --user --upgrade pipx
    python3 -m pipx ensurepath  # Add pipx to PATH
  2. Verify Installation:

    pipx --version
  3. Install a CLI Tool (e.g., poetry):

    pipx install poetry
  4. Run the Tool:

    poetry --version

Key Commands

Command Description
pipx install <pkg> Install a package globally.
pipx uninstall <pkg> Remove a package.
pipx list List installed tools.
pipx run <pkg> <cmd> Run a one-off command (e.g., pipx run black --version).

Use Cases

  • Version Management: Install and switch between multiple versions of tools like black or poetry.
  • Conflict Resolution: Safely manage tools like pre-commit, mypy, or pytest without system-wide conflicts.
  • Portability: Easily share tools across machines using pipx install --force <pkg>.

Troubleshooting

  • Permission Errors: Run python3 -m pipx ensurepath --user (macOS/Linux) or use sudo (Linux only; not recommended).
  • PATH Issues: Ensure ~/.local/bin (Linux/macOS) or ~/.local/bin (Windows) is in your PATH.
  • Upgrade pipx:
    python3 -m pip install --user --upgrade pipx