CLI for self-bootstrapped Python applications

Overview

PyApp

CI - Test CD - Publish Project - Version Project - Downloads License - Apache-2.0 OR MIT GitHub Sponsors


PyApp is a CLI wrapper for Python applications that bootstrap themselves at runtime. Each application is configured with environment variables at build time.

For a more streamlined workflow, consider using the built-in app build target of Hatch.

Table of Contents

Building

Before building your application, you must configure your project at the very least.

After you have done that, there are 2 ways to build your application.

Installation

Select the directory in which to build the executable with the --root option and run:

cargo install pyapp --force --root <DIR>

The executable will be located at <DIR>/bin/pyapp.exe if on Windows or <DIR>/bin/pyapp otherwise.

Local repository

Clone this repository then enter the cloned directory and run:

cargo build --release

The executable will be located at target/release/pyapp.exe if on Windows or target/release/pyapp otherwise. If a particular target has been set (or if cross is used since it always sets one), then the release directory will be nested one level deeper under target/<TARGET>.

Note: If you want to cross compile using cross, there is currently a limitation that requires this method of building.

Runtime behavior

Initialization

On the first run of the application:

  1. the distribution (if not embedded) will be downloaded and cached
  2. the distribution will be unpacked
  3. the project will be installed

All subsequent invocations will only check if the unpacked distribution directory exists and nothing else, to maximize CLI responsiveness.

Detection

A single environment variable called PYAPP is injected with the value of 1 (by default) when running applications and may be used to detect this mode of installation versus others.

pip

When installing or upgrading projects, pip uses isolation (by default) to provide consistent behavior on each user's machine.

Commands

Built applications have a single top-level command group named self (by default) and all other invocations will be forwarded to your actual execution logic.

Exposed

Restore
<EXE> self restore

This will wipe the unpacked distribution and start fresh.

Update
<EXE> self update

This will update the project to the latest available version in the currently used distribution.

Hidden

Metadata
<EXE> self metadata

This displays customized output based on a template.

Configuration

All configuration is done with environment variables.

Project

The desired project name and version are configured with the PYAPP_PROJECT_NAME and PYAPP_PROJECT_VERSION options, respectively. The project name must adhere to PEP 508 and will be normalized during builds according to PEP 503.

Execution mode

The following options are mutually exclusive:

Option Description
PYAPP_EXEC_MODULE This is the name of the module to execute via python -m <MODULE>
PYAPP_EXEC_SPEC This is an object reference to execute e.g. pkg.foo:cli
PYAPP_EXEC_CODE This is arbitrary code to run via python -c <CODE> (the spec option uses this internally)

If none are set then the PYAPP_EXEC_MODULE option will default to the value of PYAPP_PROJECT_NAME with hyphens replaced by underscores.

Python distribution

Known

Setting the PYAPP_PYTHON_VERSION option will determine the distribution used at runtime based on the environment at build time. If unset then the default will be the latest stable minor version of CPython.

CPython
ID
3.7
3.8
3.9
3.10
3.11

The source of distributions is the python-build-standalone project.

Some distributions have variants that may be configured with the PYAPP_DISTRIBUTION_VARIANT option:

Platform Options
Linux
  • v1
  • v2
  • v3 (default)
  • v4
Windows
  • shared (default)
  • static

Custom

You may explicitly set the PYAPP_DISTRIBUTION_SOURCE option which overrides the known distribution settings. The source must be a URL that points to an archived version of the desired Python distribution.

Setting this manually may require you to define extra metadata about the distribution that is required for accurate runtime behavior.

Format

The following formats are supported for the PYAPP_DISTRIBUTION_FORMAT option, with the default chosen based on the ending of the source URL:

Format Extensions Description
tar|gzip
  • .tar.gz
  • .tgz
A tar file with gzip compression
tar|zstd
  • .tar.zst
  • .tar.zstd
A tar file with Zstandard compression
zip
  • .zip
A ZIP file with DEFLATE compression
Python location

You may set the relative path to the Python executable after unpacking the archive with the PYAPP_DISTRIBUTION_PYTHON_PATH option. The default is python.exe on Windows and bin/python3 on all other platforms.

Embedding

You may set the PYAPP_DISTRIBUTION_EMBED option to true or 1 to embed the distribution in the executable at build time to avoid fetching it at runtime.

pip

These options have no effect when the project installation is skipped.

Extra arguments

You may set the PYAPP_PIP_EXTRA_ARGS option to provide extra arguments to the pip install command at runtime when installing or updating the project e.g. --index-url URL --only-binary :all:.

Allowing configuration

You may set the PYAPP_PIP_ALLOW_CONFIG option to true or 1 to allow the use of environment variables and other configuration at runtime.

Skipping project installation

You may set the PYAPP_SKIP_INSTALL option to true or 1 to skip installing the project in the distribution. This allows for entirely predefined distributions and thus no network calls at runtime if used in conjunction with embedding.

Installation indicator

The environment variable that is used for detection may be set to the path of the executable at runtime if you set the PYAPP_PASS_LOCATION option to true or 1. This is useful if your application wishes to in some way manage itself.

Management command name

You may set the PYAPP_SELF_COMMAND option to override the default name (self) of the management command. This is useful if you wish to have complete control of the interface or to set it to a bogus value with the intention of not using it.

Metadata template

You may set a custom template used to output metadata with the PYAPP_METADATA_TEMPLATE option which supports the following placeholders:

Placeholder Description
{project} The normalized project name
{version} The currently installed version of the project

The default template is {project} v{version} if this option is unset.

This is useful for setting custom commands for the Starship prompt. The following example configuration assumes that the built executable has been renamed to foo:

format = """
...
${custom.foo}\
...
$line_break\
...
$character"""

# <clipped>

[custom.foo]
command = "foo self metadata"
when = true
## Windows
# shell = ["cmd", "/C"]
## Other
# shell = ["sh", "--norc"]

TODO

  • Support PyPy stable versions and nightlies
  • Add a PYAPP_PIP_EXTERNAL build time option that indicates the distribution does not ship with pip and will use its standalone installation (note that this may be the default behavior in future depending on feedback)

License

PyApp is distributed under the terms of any of the following licenses:

You might also like...
a simple, non-self-describing data-interchange format.

rust-fr 'rust-fr' (aka rust for real) is a simple, non-self-describing data-interchange format. installation You can use either of these methods. Add

Standalone analytics provider and realtime dashboard designed for self-hosting.
Standalone analytics provider and realtime dashboard designed for self-hosting.

Stats Stats is a high-performance, standalone analytics provider designed for self-hosting, enabling the collection and viewing of event data from web

A simple CLI tool to create python project file structure, written in Rust
A simple CLI tool to create python project file structure, written in Rust

Ezpie Create python projects blazingly fast What Ezpie can do? It can create a python project directory What kind of directory can Ezpie create? For c

cpa is a cli tool for ultra fast setup of Rust & Python projects
cpa is a cli tool for ultra fast setup of Rust & Python projects

CPA: Create-Python-App cpa is a cli tool for ultra fast setup of new Python & Rust projects. It automates the creation of config files like style & li

A CLI tool to get help with CLI tools 🐙
A CLI tool to get help with CLI tools 🐙

A CLI tool to get help with CLI tools 🐙 halp aims to help find the correct arguments for command-line tools by checking the predefined list of common

 create and test the style and formatting of text in your terminal applications
create and test the style and formatting of text in your terminal applications

description: create and test the style and formatting of text in your terminal applications docs: https://docs.rs/termstyle termstyle is a library tha

General Rust Actix Applications and AWS Programming Utilities

RUST Actix-Web Microservice Our Rust Beginners Kit for Application Development A collection of sample code using the actix rust framework to A) Develo

Pixel-perfect integer scaling for windowed applications

integer-fullscreen Pixel-perfect integer scaling for windowed applications. Usage Run the program. Move your cursor to a window you would like to get

A tool for automating terminal applications in Unix.

expectrl A tool for automating terminal applications in Unix. Using the library you can: Spawn process Control process Expect/Verify responces It was

Comments
Releases(v0.5.0)
  • v0.5.0(May 12, 2023)

    Added:

    • Strip symbols from release builds

    Fixed:

    • Properly pass through all required environment variable options to cross compilation images
    • Properly allow configuration of the template used for the metadata command
    Source code(tar.gz)
    Source code(zip)
  • v0.4.0(May 11, 2023)

    Changed:

    • Rename PYAPP_STARSHIP_PROMPT option to PYAPP_METADATA_TEMPLATE

    Added:

    • Add PYAPP_PIP_EXTRA_ARGS option to provide extra pip install arguments
    • Add PYAPP_PIP_ALLOW_CONFIG option to allow runtime configuration of pip
    • Add configuration for correct cross compilation
    Source code(tar.gz)
    Source code(zip)
  • v0.3.1(May 10, 2023)

  • v0.3.0(May 10, 2023)

    Changed:

    • Rename PYAPP_DISTRIBUTION_COMPRESSION option to PYAPP_DISTRIBUTION_FORMAT

    Added:

    • Add --pre flag to the self update command to allow pre-release and development versions
    • Add environment variable for detection
    • Add PYAPP_SELF_COMMAND option to control the name of the management command
    • Add PYAPP_SKIP_INSTALL option to skip project installation
    • Remove dependence on OpenSSL

    Fixed:

    • Properly display error messages from pip install commands
    • Fix project version reading for the Starship prompt hidden command on non-Windows systems
    Source code(tar.gz)
    Source code(zip)
  • v0.2.0(May 8, 2023)

Owner
Ofek Lev
I like developing beautiful APIs.
Ofek Lev
Guardian Self Assessment CLI tool

Guardian Self Assessment CLI tool What? self-assessment is a tool that generates a list of PRs authored and reviewed by you. Why? Assessing oneself is

The Guardian 5 Jul 6, 2022
Rust Imaging Library's Python binding: A performant and high-level image processing library for Python written in Rust

ril-py Rust Imaging Library for Python: Python bindings for ril, a performant and high-level image processing library written in Rust. What's this? Th

Cryptex 13 Dec 6, 2022
Update informer for CLI applications written in Rust 🦀

Update-informer Update informer for CLI applications written in Rust ?? Usage Add to Cargo.toml: [dependencies] update-notifier = "0.1.0" To check the

Grachev Mikhail 166 Dec 18, 2022
Terminal plotting library for using in Rust CLI applications

textplots Terminal plotting library for using in Rust CLI applications. Should work well in any unicode terminal with monospaced font. It is inspired

Alexey Suslov 163 Dec 30, 2022
CarLI is a framework for creating single-command and multi-command CLI applications in Rust

CarLI is a framework for creating single-command and multi-command CLI applications in Rust. The framework provides error and IO types better suited for the command line environment, especially in cases where unit testing is needed.

Kevin Herrera 3 Jan 21, 2022
An extensible plotting library for CLI applications.

termplot An extensible plotting library for CLI applications. Quick Start Documentation Examples Plotting a function Historigram Composing multiple pl

Xavier Hamel 3 Jan 1, 2023
Self-contained template system with Handlebars and inline shell scripts

Handlematters Self-contained template system with Handlebars and inline shell scripts Introduction Handlematters is a template system that combines Ha

Keita Urashima 3 Sep 9, 2022
Work-in-progress Rust application that converts C++ header-only libraries to single self-contained headers.

unosolo Work-in-progress Rust application that converts C++ header-only libraries to single self-contained headers. Disclaimer This is my first Rust p

Vittorio Romeo 26 Jul 9, 2021
A self-contained, unopinionated, fast and lightweight executable launcher.

Kickoff ?? A self-contained, unopinionated, fast and lightweight executable launcher. Supported Platforms Platform Host Target aarch64-apple-macos-non

Nimbus 18 Oct 27, 2023
Temporary elevated access management as a self-hosted service

????☁️ S A T O U N K I Temporary elevated access management as a self-hosted service Overview Satounki is a self-hosted service which brings visibilit

جاد 31 Dec 17, 2023