Introduction to Pipx
Pipx is a command-line tool to help you install and run end-user applications written in Python. It allows you to run Python applications in isolated environments and ensures the installed applications are kept outside your main Python environment.
Installation
pip install pipx pipx ensurepath
Basic Usage
Installing a Python application globally:
pipx install PACKAGE
Running a Python application within an isolated environment:
pipx run APP [ARGS...]
Here’s how to use pipx to install and run httpie:
pipx install httpie http GET https://api.github.com
Advanced Usage
Upgrading a package:
pipx upgrade PACKAGE
Uninstalling a package:
pipx uninstall PACKAGE
Listing installed packages:
pipx list
Reinstalling all packages:
pipx reinstall-all
Running binaries within an isolated environment without installing them:
pipx run --spec SPEC PATH [ARGS...]
API Examples
Running a script for a specific package:
pipx runpip PACKAGE install SOME_PACKAGE
Viewing a package’s dependencies:
pipx show PACKAGE
Inject additional packages into an existing virtual environment of a pipx-managed application:
pipx inject PACKAGE DEPENDENCY_PACKAGE
Setting environment variables for isolated environments:
PIPX_ALWAYS_COPY=1 pipx install some_package
App Example
Let’s create an example application using pipx. First, we need a Python script. Save the following script in a file named hello.py
:
def main(): print("Hello, World!") if __name__ == "__main__": main()
Turn this script into an executable using an entry point in setup.py
:
from setuptools import setup setup( name="hello_app", version="0.1", py_modules=["hello"], entry_points={ "console_scripts": [ "hello=hello:main", ], }, )
Now you can install and run this application using pipx:
pipx install /path/to/your/hello_app hello
This will display:
Hello, World!
By using pipx, you ensure that your application runs in an isolated environment, preventing dependency conflicts and keeping your main Python environment clean.
Hash: 1e4ab8abb8b720c2e01cfa62e24649628f22c4a57c789e10456efbb7d1dc5e86