Install and Execute Python Applications Using pipx :
by:
blow post content copied from Real Python
click here to view original post
A straightforward way to distribute desktop and command-line applications written in Python is to publish them on the Python Package Index (PyPI), which hosts hundreds of thousands of third-party packages. Many of these packages include runnable scripts, but using them requires decent familiarity with the Python ecosystem. With pipx
, you can safely install and execute such applications without affecting your global Python interpreter.
In this tutorial, you’ll learn how to:
- Turn the Python Package Index (PyPI) into an app marketplace
- Run installed applications without explicitly calling Python
- Avoid dependency conflicts between different applications
- Try throw-away applications in temporary locations
- Manage the installed applications and their environments
To fully benefit from this tutorial, you should feel comfortable around the terminal. In particular, knowing how to manage Python versions, create virtual environments, and install third-party modules in your projects will go a long way.
Note: If you’re a Windows user, then it’s highly recommended you follow our Python coding setup guide before plunging into this tutorial. The gist of it is that you should avoid installing Python from the Microsoft Store, as it could prevent pipx
from working correctly.
To help you get to grips with pipx
, you can download the supplemental materials, which include a handy command cheat sheet. Additionally, you can test your understanding by taking a short quiz.
Get Your Cheatsheet: Click here to download the free cheatsheet of pipx commands you can use to install and execute Python applications.
Take the Quiz: Test your knowledge with our interactive “Install and Execute Python Applications Using pipx” quiz. Upon completion you will receive a score so you can track your learning progress over time:
Get Started With pipx
On the surface, pipx
resembles pip
because it also lets you install Python packages from PyPI or another package index. However, unlike pip
, it doesn’t install packages into your system-wide Python interpreter or even an activated virtual environment. Instead, it automatically creates and manages virtual environments for you to isolate the dependencies of every package that you install.
Additionally, pipx
adds symbolic links to your PATH
variable for every command-line script exposed by the installed packages. As a result, you can invoke those scripts directly from the command line without explicitly running them through the Python interpreter.
Think of pipx
as Python’s equivalent of npx
in the JavaScript ecosystem. Both tools let you install and execute third-party modules in the command line just as if they were standalone applications. However, not all modules are created equal.
Broadly speaking, you can classify the code distributed through PyPI into three categories:
- Importable: It’s either pure-Python source code or Python bindings of compiled shared objects that you want to import in your Python projects. Typically, they’re libraries like Requests or Polars, providing reusable pieces of code to help you solve a common problem. Alternatively, they might be frameworks like FastAPI or PyGame that you build your applications around.
- Runnable: These are usually command-line utility tools like
black
,isort
, orflake8
that assist you during the development phase. They could also be full-fledged applications likebpython
or the JupyterLab environment, which is primarily implemented in a foreign TypeScript programming language. - Hybrid: They combine both worlds by providing importable code and runnable scripts at the same time. Flask and Django are good examples, as they offer utility scripts while remaining web frameworks for the most part.
Making a distribution package runnable or hybrid involves defining one or more entry points in the corresponding configuration file. Historically, these would be setup.py
or setup.cfg
, but modern build systems in Python should generally rely on the pyproject.toml
file and define their entry points in the [project.scripts]
TOML table.
Note: If you use Poetry to manage your project’s dependencies, then you can add the appropriate script declarations in the tool-specific [tool.poetry.scripts]
table.
Each entry point represents an independent script that you can run by typing its name at the command prompt. For example, if you’ve ever used the django-admin
command, then you’ve called out an entry point to the Django framework.
Note: Don’t confuse entry points, which link to individual functions or callables in your code, with runnable Python packages that rely on the __main__
module to provide a command-line interface.
For example, Rich is a library of building blocks for creating text-based user interfaces in Python. At the same time, you can run this package with python -m rich
to display a demo application that illustrates various visual components at your fingertips. Despite this, pipx
won’t recognize it as runnable because the library doesn’t define any entry points.
To sum up, the pipx
tool will only let you install Python packages with at least one entry point. It’ll refuse to install runnable packages like Rich and bare-bones libraries that ship Python code meant just for importing.
Once you identify a Python package with entry points that you’d like to use, you should first create and activate a dedicated virtual environment as a best practice. By keeping the package isolated from the rest of your system, you’ll eliminate the risk of dependency conflicts across various projects that might require the same Python library in different versions. Furthermore, you won’t need the superuser permissions to install the package.
Deciding where and how to create a virtual environment and then remembering to activate it every time before running the corresponding script can become a burden. Fortunately, pipx
automates these steps and provides even more features that you’ll explore in this tutorial. But first, you need to get pipx
running itself.
Test Drive pipx
Without Installation
If you’re unsure whether pipx
will address your needs and would prefer not to commit to it until you’ve properly tested the tool, then there’s good news! Thanks to a self-contained executable available for download, you can give pipx
a spin without having to install it.
To get that executable, visit the project’s release page on the official GitHub repository in your web browser and grab the latest version of a file named pipx.pyz
. Files with the .pyz
extension represent runnable Python ZIP applications, which are essentially ZIP archives containing Python source code and some metadata, akin to JAR files in Java. They can optionally vendor third-party dependencies that you’d otherwise have to install by hand.
Note: Internally, the pipx
project uses shiv
to build its Python ZIP application. When you first run a Python ZIP application that was built with shiv
, it’ll unpack itself into a hidden folder named .shiv/
located in your user’s home directory. As a result, subsequent runs of the same application will reuse the already extracted files, speeding up the startup time.
Afterward, you can run pipx.pyz
by passing the path to your downloaded copy of the file to your Python interpreter—just as you would with a regular Python script:
Read the full article at https://realpython.com/python-pipx/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
April 03, 2024 at 07:30PM
Click here for more details...
=============================
The original post is available in Real Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================
Post a Comment