Home Python Pipx – Install And Run Python Applications In Isolated Environments

Pipx – Install And Run Python Applications In Isolated Environments

By sk
Published: Updated: 8K views

It is always recommended to install Python applications in virtual environments to avoid conflicts between them. The `pip` package manager allows us to install Python applications in isolated environments using two tools: `venv` and `virtualenv`. Another Python package manager, `Pipenv`, is recommended by Python.org for installing Python applications. Unlike `pip`, `Pipenv` automatically creates virtual environments by default, so you don’t need to manually create them for your projects.

Today, I discovered a similar tool called `Pipx`, a free and open-source utility that lets you install and run Python applications in isolated virtual environments.

With `Pipx`, you can easily install thousands of Python applications hosted on PyPI without much hassle. The best part is that you can do everything with regular user permissions—no need to be the "root" user or have `sudo` permissions. It's also worth mentioning that `Pipx` can run a program from a temporary environment without installing it, which is handy when you frequently test multiple versions of the same program. The packages installed with `Pipx` can be listed, upgraded, or uninstalled at any time. `Pipx` is cross-platform, so it works on Linux, macOS, and Windows.

Install Pipx in Linux

Python 3.6+, Pip and venv module are required to install pipx. Make sure you have installed them as described in the following guide.

Here, venv is needed to create virtual environments.

Next, run the following commands to install Pipx.

$ python3 -m pip install --user pipx
$ python3 -m pipx ensurepath

The default location of pipx's binary is ~/.local/bin. You can override this with the PIPX_BIN_DIR environment variable. If you override PIPX_BIN_DIR, just make sure it is on your path by running userpath append $PIPX_BIN_DIR.

And the default virtual environment location of Pipx is ~/.local/pipx. This can be overridden with the environment variable PIPX_HOME.

Let us go ahead and see how to install Python applications using Pipx.

Install and Run Python Applications in Isolated Environments using Pipx

Here are few examples to getting started with Pipx.

Install Python Packages using Pipx

To install a Python application, for example cowsay, globally, run:

$ pipx install cowsay

This command will automatically create virtual environments, install the package in it and put the package executable file on your $PATH.

Sample output:

installed package cowsay 2.0.3, Python 3.6.8
These binaries are now globally available
- cowsay
done! ✨ ? ✨
Install Python Applications Using Pipx

Install Python Applications Using Pipx

Let us test newly installed cowsay program:

Test Python application

Test Python application

Here, I have taken the examples from official site. You can install/test any other Python package of your choice.

List Python Packages

To list all installed applications using Pipx, run:

$ pipx list

Sample output:

venvs are in /home/sk/.local/pipx/venvs
binaries are exposed on your $PATH at /home/sk/.local/bin
package cowsay 2.0.3, Python 3.6.8
- cowsay

If you have not installed any packages, you will see the following output:

nothing has been installed with pipx ?

Upgrade Packages

To upgrade a package, simply do:

$ pipx upgrade cowsay

To upgrade all installed packages in one go, use:

$ pipx upgrade-all

Upgrade Pipx

To upgrade to latest avaialble version, run:

$ python3 -m pip install --user -U pipx

Run an Application from Temporary Virtual Environments

At times, you might want to run a specific python program, but not actually install it.

$ pipx run pycowsay moooo
Run Python Applications In Isolated Environments Using Pipx

Run Python Applications In Temporary Isolated Virtual Environments

This command doesn't actually install the given program, but run it from the temporary virtual environment. You can use this command for quickly testing a python application.

You can even run .py files directly as well.

$ pipx run https://gist.githubusercontent.com/cs01/fa721a17a326e551ede048c5088f9e0f/raw/6bdfbb6e9c1132b1c38fdd2f195d4a24c540c324/pipx-demo.py
pipx is working!

Uninstall packages

A package can be uninstalled with command:

$ pipx uninstall cowsay

To remove all installed packages:

$ pipx uninstall-all

Getting help

To view help section, run:

$ pipx --help

And, that's all. If you're ever looking for a safe, convenient and reliable application to install and run Python applications, Pipx might be a good choice.

Resource:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More