Pipx: Safely Install Packages Globally

Installing Python packages globally using pip can cause several issues. However, some packages are better installed globally on your system because they are tools you use daily. This is in contrast to your regular Python project dependencies, for which you would typically use something like Poetry or Pipenv. This article explores the pipx tool and teaches you when it’s typically used.

Issues you may run into

Here are a few common issues that occur when you use pip install to install tools globally on your system:

  1. Dependency Conflicts: The most common problem is conflicting dependencies. For example, if two packages require different versions of the same dependency, installing both packages globally could cause one or both to break. This is also known as “dependency hell.”
  2. System Breakage: On some systems, certain system utilities rely on Python and its packages.
    • If you install a package globally and it updates or replaces a dependency used by a system utility, it can break that utility.
    • Vice versa: if you update your system, your globally installed package can break because it was installed for other Python versions and other versions of the dependencies.
  3. Difficulty Isolating Issues: When all packages are installed globally, it can be difficult to determine which package is causing a problem if something goes wrong.
  4. Not Easy to Remove or Update Packages: Uninstalling or updating a package can cause issues with other packages that depend on it. Removing a package does not remove its dependencies, leaving unused clutter on your system.
  5. Lack of Isolation: Global packages are available to all Python projects on your system, whether or not they need them, leading to potential version conflicts between projects.

There are multiple ways to prevent these issues, and they all involve using virtual environments. If you’re new to virtual environments, please click the link to read up on it first.

Identifying your problem

Before deciding which tool to use, we must identify the problem you’re trying to solve. Let’s define two main categories:

  1. Your Python project needs a dependency like FastAPI, Django, NumPy, or Requests
  2. You want to install a Python-based tool that you regularly start from the command line, like Jupyter Notebook, Poetry, or Pipenv

For the first type of problem, you should create a virtual environment to accompany your project. You can do so manually with python -m venv or use popular tools like Poetry, or Pipenv. For the second type of problem, pipx is your go-to tool!

What is pipx?

pipx is a utility for installing and running Python applications in isolated environments. It’s a great tool for Python developers and users who want to try out different applications without worrying about dependency conflicts. Here are some of the advantages of using pipx:

  1. Isolation of Dependencies: Each application installed with pipx gets its own isolated environment, which means its dependencies won’t interfere with those of your other applications or your global Python environment. This reduces the risk of package conflicts.
  2. Safe Experimentation: Because each application has its own isolated environment, you can try out different applications or different versions of the same application without affecting your other installations.
  3. Global Access: Applications installed with pipx are accessible from anywhere on your system, just like applications installed globally with pip, but without the associated risks of package conflicts.
  4. Easy Uninstallation: If you no longer need an application, you can remove it completely and cleanly with a single command, and it won’t leave behind any clutter or unused dependencies.
  5. Run-Once Applications: pipx can run Python applications in a temporary environment, which is useful for running applications that you only need to use once and don’t want to install permanently.
  6. Keeps your System Clean: Since every package is isolated, your system Python environment remains clean and uncluttered.
  7. Manageable Upgrade/Downgrade: Each package installed with pipx can be easily upgraded or downgraded without affecting other packages.
  8. Safe for system Python: Installing packages globally with pip can risk breaking system utilities that depend on Python. Since pipx isolates packages, it avoids this risk.

All of these features make pipx a valuable tool for Python developers and users who want to maintain a clean, conflict-free system while still being able to install and use a variety of Python applications easily.

When pipx is typically used

In short, pipx is typically used to install and run Python tools and applications, while other tools focus on managing venvs and installing library dependencies for your Python projects. To get a better feeling of when to use pipx, here are some Python tools that you might install globally:

  1. Black: A popular Python code formatter.
  2. Flake8: A tool for checking Python code against some of the style conventions in PEP 8.
  3. Mypy: A static type checker for Python.
  4. Pipenv and Poetry: tools for dependency management and packaging in Python.
  5. HTTPie: A command line HTTP client, a user-friendly cURL replacement.
  6. Glances: A cross-platform system monitoring tool.
  7. Jupyter: An open-source web application that allows the creation and sharing of documents that contain live code, equations, visualizations, and narrative text.
  8. Youtube-dl: A command-line program to download videos from YouTube and other sites.
  9. Cookiecutter: A command-line utility that creates projects from project templates.

How to install pipx

There are a number of methods to install pipx, depending on your OS and the type of your Python installation. I advise you to read the official installation instructions for your OS, but here’s a short summary to get you started quickly.

Windows

On windows, you typically use the regular pip tool to install pipx:

pip install --user pipxCode language: Python (python)

You may get a warning about pipx not being in your PATH. If that happens, first cd to the installation location of pipx and then run this:

pipx ensurepathCode language: Python (python)

This will add the above-mentioned path and %USERPROFILE%\.local\bin to your search path. Don’t forget to restart your terminal session after these steps to apply the changes.

MacOS

On MacOS, you typically want to use Homebrew:

brew install pipx
pipx ensurepathCode language: Shell Session (shell)

We used the ensurepath command again to make sure pipx will be in your PATH.

Linux

On Linux, you may want to see if pipx is offered by your package manager. E.g., on Debian-like systems (Debian, Ubuntu, Mint) you can use sudo apt install pipx. Your OS will keep pipx updated and secure.

If you want to have the most recent version of pipx, you’re better off using pip install:

pip install --user pipx
python3 -m pipx ensurepathCode language: Shell Session (shell)

We used the ensurepath command again to make sure pipx will be in your PATH.

How to use pipx

While the official documentation covers all the commands and options, I’ll only show you the most important ones here.

Install a package

You can install a new package with pipx install. For example, to install Black, you would use:

pipx install blackCode language: Shell Session (shell)

This command installs the package in its own isolated environment, separate from your system Python and any other packages installed with pipx.

List installed packages

You can list all packages installed with pipx using the pipx list command:

pipx listCode language: Shell Session (shell)

This will give you a list of all the packages you’ve installed with pipx, along with their versions and the versions of Python they’re using.

Upgrade a package

If you want to upgrade a package to its latest version, you can use pipx upgrade. For example, to upgrade Black, you would use:

pipx upgrade blackCode language: Shell Session (shell)

Uninstall a package

If you decide you no longer need a package, you can uninstall it with pipx uninstall. For example, to uninstall Black, you would use:

pipx uninstall blackCode language: Shell Session (shell)

Inject a package

If an application installed by pipx requires additional packages, you can add them with pipx inject. For example, if you have ipython installed and want to add the matplotlib package to it, you would use:

pipx inject ipython matplotlibCode language: Shell Session (shell)

Reinstall all packages

If you upgrade your system’s Python version and want all your pipx-installed packages to use the new version, you can use pipx reinstall-all:

pipx reinstall-allCode language: Python (python)

This is my personal favorite and a big time saver. Each time I update my Ubuntu installation, which is each half year, I have to fix all my globally installed Python tools. With pipx, it’s just this one command, and you’re all set!

Run a package

If you want to run a Python application in a one-off environment without installing it, you can use pipx run. For example, to run Black just once on a file named example.py, you would use:

pipx run black example.pyCode language: Python (python)

As another example, let’s say you want to download a YouTube video using youtube-dl, but you don’t have youtube-dl installed on your system and don’t plan to use it frequently. You could use pipx run to execute youtube-dl in a temporary environment:

pipx run youtube-dl https://www.youtube.com/watch?v=<video_id>Code language: Python (python)

This command will create a temporary environment, install youtube-dl in that environment, download the video from the provided URL and then clean up the environment when it’s done.

Get certified with our courses

Learn Python properly through small, easy-to-digest lessons, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.

Related articles

Leave a Comment

Python Newsletter

Before you leave, you may want to sign up for my newsletter.

Tips & Tricks
News
Course discounts

Googler exit intent popup

No spam & you can unsubscribe at any time.