A powerful tool for managing Python command-line interface (CLI) applications is Pipx. It not only allows you to install and run Python applications in isolated environments, it also ensures that each application has its own set of dependencies, independent of your global Python environment.
Codemagnet is here to help you cover everything you need to know about Pipx, from installation to practical usage, along with coding examples to get you started.
Why Use Pipx?
Before diving into the details, let’s explore why Pipx is a valuable tool for Python developers:
- Isolated Environments: Each CLI application is installed in its own virtual environment, preventing dependency conflicts.
- Ease of Use: Simplifies the process of installing, running, and managing CLI applications.
- Security: Reduces the risk of dependency issues affecting your global Python environment.
Installing Pipx
To install Pipx, you need Python 3.6 or later. You can install Pipx using pip:

pip install pipx
After installing Pipx, you need to ensure that its binary path is in your system’s PATH. You can do this by running:
pipx ensurepath
Basic Usage of Pipx
Installing a Python CLI Application
To install a Python CLI application, use the install command. For example, to install httpie, a user-friendly command-line HTTP client:
pipx install httpie
This command creates an isolated virtual environment for httpie and installs it there. You can now run httpie from anywhere in your terminal:
httpie https://jsonplaceholder.typicode.com/posts/1
Running a Python CLI Application without Installing
Sometimes, you might want to run a CLI application without installing it permanently. You can achieve this using the run command. For example, to run black, a popular Python code formatter, without installing it:
pipx run black my_script.py
Upgrading a Python CLI Application
To upgrade an installed CLI application, use the upgrade command. For instance, to upgrade httpie:
pipx upgrade httpie
Uninstalling a Python CLI Application
To uninstall an application, use the uninstall command. For example, to uninstall httpie:
pipx uninstall httpie
Advanced Pipx Usage
Installing a Specific Version
If you need a specific version of a CLI application, you can specify it during installation. For example, to install version 1.1.0 of httpie:
pipx install httpie==1.1.0
Listing Installed Applications
To see a list of all applications installed via Pipx, use the list command:
pipx list
Injecting Dependencies into an Application’s Virtual Environment
If you need to add extra dependencies to an application’s virtual environment, use the inject command. For example, to add requests to httpie’s environment:
pipx inject httpie requests
Running Python Scripts with Pipx
You can also use Pipx to run standalone Python scripts with isolated dependencies. This is particularly useful for scripts that require specific packages. For example, to run a script that requires requests:
pipx runpip requests requests
pipx runpython my_script.py
Example: Creating and Managing a Custom CLI Application
Let’s create a simple Python CLI application and manage it using Pipx.
- Create the CLI Application
First, create a new Python project with the following structure:
mycli/
├── mycli/
│ └── __main__.py
└── setup.py
In mycli/__main__.py, write a simple script:
# mycli/__main__.py
import click
@click.command()
@click.argument('name')
def hello(name):
"""Say hello to NAME."""
click.echo(f'Hello, {name}!')
if __name__ == '__main__':
hello()
In setup.py, define the package and entry point:
# setup.py
from setuptools import setup, find_packages
setup(
name='mycli',
version='0.1',
packages=find_packages(),
install_requires=[
'click',
],
entry_points={
'console_scripts': [
'mycli=mycli.__main__:hello',
],
},
)
- Install and Run the Application with Pipx
Navigate to the project directory and install the application using Pipx:
pipx install .
Now you can run your CLI application from anywhere:
mycli YourName
This should output:
Hello, YourName!
Conclusion
Pipx is a versatile and powerful tool for managing Python CLI applications. It ensures that each application runs in its own isolated environment, preventing dependency conflicts and enhancing security. By following this guide, you can efficiently install, run, and manage CLI applications, as well as create your own. Embrace Pipx to streamline your workflow and enhance your Python development experience.





Leave a Reply