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
docs: add standalone application guide with Briefcase (#3315)Rowlando13before adoptiona7d2a8d
lines 1–148why, one step back1# Standalone Application with Briefcase23[Briefcase](https://briefcase.beeware.org/) is a tool for packaging Python4projects as standalone native applications. It can produce installers and5executables for macOS, Windows, and Linux that do not require the user to6install Python or any dependencies.78- Produces platform-native installers (``.pkg`` on macOS, ``.msi`` on9 Windows, ``.deb``/``.rpm`` on Linux).10- Bundles a Python interpreter and all dependencies.11- Supports passing command line arguments to the app.1213This page outlines the basics of packaging a Click application with14Briefcase. Be sure to read its15[documentation](https://briefcase.beeware.org/en/stable/how-to/building/cli-apps/)16and use ``briefcase --help`` to understand what features are available.1718## Installation1920Install Briefcase in the virtual environment:2122```console23pip install briefcase24```2526## Configuration2728Add a ``[tool.briefcase]`` section and a29``[tool.briefcase.app.<app-name>]`` section to ``pyproject.toml``.30Set ``console_app = true`` so Briefcase treats the project as a command31line application rather than a GUI application.3233Given a Click application with the following project structure:3435```text36hello-cli/37 src/38 hello_cli/39 __init__.py40 app.py41 LICENSE42 pyproject.toml43```4445Where ``app.py`` contains a Click command:4647```python48import click4950@click.command()51@click.argument("name", default="World")52@click.option("--count", default=1, help="Number of times to greet.")53def main(name, count):54 """Greet someone by NAME (default: World)."""55 for _ in range(count):56 click.echo(f"Hello, {name}!")57```5859Add the following Briefcase configuration to ``pyproject.toml``:6061```toml62[tool.briefcase]63project_name = "Hello CLI"64bundle = "com.example"65version = "0.0.1"66url = "https://example.com/hello-cli"67license.file = "LICENSE"68author = "Your Name"69author_email = "you@example.com"7071[tool.briefcase.app.hello-cli]72formal_name = "Hello CLI"73description = "My first application"74long_description = """More details about the app should go here.75"""76sources = [77 "src/hello_cli",78]79console_app = true80requires = [81 "click",82]83```8485The key settings are:8687- ``console_app = true`` -- tells Briefcase this is a terminal88 application, not a GUI.89- ``sources`` -- the list of source packages to include.90- ``requires`` -- the Python dependencies to bundle (Click and any other91 libraries the project needs).9293## Entry Point9495Briefcase launches the application by running the package with96``python -m <package>``, so a ``__main__.py`` file **must** exist in the97package. Without it, Briefcase will not be able to start the application.9899Create ``__main__.py`` in the package directory and call the Click100command:101102```python103from hello_cli.app import main104105if __name__ == "__main__":106 main()107```108109## Running110111Use ``briefcase dev`` to run the application directly from the source112tree. Pass command line arguments after ``--``:113114```console115$ briefcase dev -- World116Hello, World!117$ briefcase dev -- World --count 2118Hello, World!119Hello, World!120```121122## Building and Packaging123124To create a distributable executable, run the following commands:125126```console127briefcase create128briefcase build129briefcase package130```131132``briefcase create`` downloads a Python interpreter and installs133dependencies into an isolated app bundle. ``briefcase build`` compiles134the app, and ``briefcase package`` produces the final platform installer.135136On macOS, this produces a ``.pkg`` installer. On Windows, it produces a137``.msi`` installer. On Linux, it produces a system package (``.deb``,138``.rpm``, etc.) for the current distribution.139140Once installed, users can run the application directly from the terminal:141142```console143$ hello-cli World144Hello, World!145$ hello-cli World --count 2146Hello, World!147Hello, World!148```