standalone-apps.md
This file
Path
docs/standalone-apps.md
148 lines · 3.7 KiB · Markdown
Last changed by
docs: add standalone application guide with Briefcase (#3315)Rowlando13before adoptiona7d2a8d
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149# Standalone Application with Briefcase
[Briefcase](https://briefcase.beeware.org/) is a tool for packaging Pythonprojects as standalone native applications. It can produce installers andexecutables for macOS, Windows, and Linux that do not require the user toinstall Python or any dependencies.
- Produces platform-native installers (``.pkg`` on macOS, ``.msi`` on Windows, ``.deb``/``.rpm`` on Linux).- Bundles a Python interpreter and all dependencies.- Supports passing command line arguments to the app.
This page outlines the basics of packaging a Click application withBriefcase. Be sure to read its[documentation](https://briefcase.beeware.org/en/stable/how-to/building/cli-apps/)and use ``briefcase --help`` to understand what features are available.
## Installation
Install Briefcase in the virtual environment:
```consolepip install briefcase```
## Configuration
Add a ``[tool.briefcase]`` section and a``[tool.briefcase.app.<app-name>]`` section to ``pyproject.toml``.Set ``console_app = true`` so Briefcase treats the project as a commandline application rather than a GUI application.
Given a Click application with the following project structure:
```texthello-cli/ src/ hello_cli/ __init__.py app.py LICENSE pyproject.toml```
Where ``app.py`` contains a Click command:
```pythonimport click
@click.command()@click.argument("name", default="World")@click.option("--count", default=1, help="Number of times to greet.")def main(name, count): """Greet someone by NAME (default: World).""" for _ in range(count): click.echo(f"Hello, {name}!")```
Add the following Briefcase configuration to ``pyproject.toml``:
```toml[tool.briefcase]project_name = "Hello CLI"bundle = "com.example"version = "0.0.1"url = "https://example.com/hello-cli"license.file = "LICENSE"author = "Your Name"author_email = "you@example.com"
[tool.briefcase.app.hello-cli]formal_name = "Hello CLI"description = "My first application"long_description = """More details about the app should go here."""sources = [ "src/hello_cli",]console_app = truerequires = [ "click",]```
The key settings are:
- ``console_app = true`` -- tells Briefcase this is a terminal application, not a GUI.- ``sources`` -- the list of source packages to include.- ``requires`` -- the Python dependencies to bundle (Click and any other libraries the project needs).
## Entry Point
Briefcase launches the application by running the package with``python -m <package>``, so a ``__main__.py`` file **must** exist in thepackage. Without it, Briefcase will not be able to start the application.
Create ``__main__.py`` in the package directory and call the Clickcommand:
```pythonfrom hello_cli.app import main
if __name__ == "__main__": main()```
## Running
Use ``briefcase dev`` to run the application directly from the sourcetree. Pass command line arguments after ``--``:
```console$ briefcase dev -- WorldHello, World!$ briefcase dev -- World --count 2Hello, World!Hello, World!```
## Building and Packaging
To create a distributable executable, run the following commands:
```consolebriefcase createbriefcase buildbriefcase package```
``briefcase create`` downloads a Python interpreter and installsdependencies into an isolated app bundle. ``briefcase build`` compilesthe app, and ``briefcase package`` produces the final platform installer.
On macOS, this produces a ``.pkg`` installer. On Windows, it produces a``.msi`` installer. On Linux, it produces a system package (``.deb``,``.rpm``, etc.) for the current distribution.
Once installed, users can run the application directly from the terminal:
```console$ hello-cli WorldHello, World!$ hello-cli World --count 2Hello, World!Hello, World!```