| 1 |
# Standalone Application with Briefcase |
| 2 |
|
| 3 |
[Briefcase](https://briefcase.beeware.org/) is a tool for packaging Python |
| 4 |
projects as standalone native applications. It can produce installers and |
| 5 |
executables for macOS, Windows, and Linux that do not require the user to |
| 6 |
install Python or any dependencies. |
| 7 |
|
| 8 |
- Produces platform-native installers (``.pkg`` on macOS, ``.msi`` on |
| 9 |
Windows, ``.deb``/``.rpm`` on Linux). |
| 10 |
- Bundles a Python interpreter and all dependencies. |
| 11 |
- Supports passing command line arguments to the app. |
| 12 |
|
| 13 |
This page outlines the basics of packaging a Click application with |
| 14 |
Briefcase. Be sure to read its |
| 15 |
[documentation](https://briefcase.beeware.org/en/stable/how-to/building/cli-apps/) |
| 16 |
and use ``briefcase --help`` to understand what features are available. |
| 17 |
|
| 18 |
## Installation |
| 19 |
|
| 20 |
Install Briefcase in the virtual environment: |
| 21 |
|
| 22 |
```console |
| 23 |
pip install briefcase |
| 24 |
``` |
| 25 |
|
| 26 |
## Configuration |
| 27 |
|
| 28 |
Add a ``[tool.briefcase]`` section and a |
| 29 |
``[tool.briefcase.app.<app-name>]`` section to ``pyproject.toml``. |
| 30 |
Set ``console_app = true`` so Briefcase treats the project as a command |
| 31 |
line application rather than a GUI application. |
| 32 |
|
| 33 |
Given a Click application with the following project structure: |
| 34 |
|
| 35 |
```text |
| 36 |
hello-cli/ |
| 37 |
src/ |
| 38 |
hello_cli/ |
| 39 |
__init__.py |
| 40 |
app.py |
| 41 |
LICENSE |
| 42 |
pyproject.toml |
| 43 |
``` |
| 44 |
|
| 45 |
Where ``app.py`` contains a Click command: |
| 46 |
|
| 47 |
```python |
| 48 |
import click |
| 49 |
|
| 50 |
@click.command() |
| 51 |
@click.argument("name", default="World") |
| 52 |
@click.option("--count", default=1, help="Number of times to greet.") |
| 53 |
def main(name, count): |
| 54 |
"""Greet someone by NAME (default: World).""" |
| 55 |
for _ in range(count): |
| 56 |
click.echo(f"Hello, {name}!") |
| 57 |
``` |
| 58 |
|
| 59 |
Add the following Briefcase configuration to ``pyproject.toml``: |
| 60 |
|
| 61 |
```toml |
| 62 |
[tool.briefcase] |
| 63 |
project_name = "Hello CLI" |
| 64 |
bundle = "com.example" |
| 65 |
version = "0.0.1" |
| 66 |
url = "https://example.com/hello-cli" |
| 67 |
license.file = "LICENSE" |
| 68 |
author = "Your Name" |
| 69 |
author_email = "[email protected]" |
| 70 |
|
| 71 |
[tool.briefcase.app.hello-cli] |
| 72 |
formal_name = "Hello CLI" |
| 73 |
description = "My first application" |
| 74 |
long_description = """More details about the app should go here. |
| 75 |
""" |
| 76 |
sources = [ |
| 77 |
"src/hello_cli", |
| 78 |
] |
| 79 |
console_app = true |
| 80 |
requires = [ |
| 81 |
"click", |
| 82 |
] |
| 83 |
``` |
| 84 |
|
| 85 |
The key settings are: |
| 86 |
|
| 87 |
- ``console_app = true`` -- tells Briefcase this is a terminal |
| 88 |
application, not a GUI. |
| 89 |
- ``sources`` -- the list of source packages to include. |
| 90 |
- ``requires`` -- the Python dependencies to bundle (Click and any other |
| 91 |
libraries the project needs). |
| 92 |
|
| 93 |
## Entry Point |
| 94 |
|
| 95 |
Briefcase launches the application by running the package with |
| 96 |
``python -m <package>``, so a ``__main__.py`` file **must** exist in the |
| 97 |
package. Without it, Briefcase will not be able to start the application. |
| 98 |
|
| 99 |
Create ``__main__.py`` in the package directory and call the Click |
| 100 |
command: |
| 101 |
|
| 102 |
```python |
| 103 |
from hello_cli.app import main |
| 104 |
|
| 105 |
if __name__ == "__main__": |
| 106 |
main() |
| 107 |
``` |
| 108 |
|
| 109 |
## Running |
| 110 |
|
| 111 |
Use ``briefcase dev`` to run the application directly from the source |
| 112 |
tree. Pass command line arguments after ``--``: |
| 113 |
|
| 114 |
```console |
| 115 |
$ briefcase dev -- World |
| 116 |
Hello, World! |
| 117 |
$ briefcase dev -- World --count 2 |
| 118 |
Hello, World! |
| 119 |
Hello, World! |
| 120 |
``` |
| 121 |
|
| 122 |
## Building and Packaging |
| 123 |
|
| 124 |
To create a distributable executable, run the following commands: |
| 125 |
|
| 126 |
```console |
| 127 |
briefcase create |
| 128 |
briefcase build |
| 129 |
briefcase package |
| 130 |
``` |
| 131 |
|
| 132 |
``briefcase create`` downloads a Python interpreter and installs |
| 133 |
dependencies into an isolated app bundle. ``briefcase build`` compiles |
| 134 |
the app, and ``briefcase package`` produces the final platform installer. |
| 135 |
|
| 136 |
On macOS, this produces a ``.pkg`` installer. On Windows, it produces a |
| 137 |
``.msi`` installer. On Linux, it produces a system package (``.deb``, |
| 138 |
``.rpm``, etc.) for the current distribution. |
| 139 |
|
| 140 |
Once installed, users can run the application directly from the terminal: |
| 141 |
|
| 142 |
```console |
| 143 |
$ hello-cli World |
| 144 |
Hello, World! |
| 145 |
$ hello-cli World --count 2 |
| 146 |
Hello, World! |
| 147 |
Hello, World! |
| 148 |
``` |