| 1 |
# Accessing Briefcase packaging metadata at runtime { #access-packaging-metadata } |
| 2 |
|
| 3 |
When Briefcase installs your app, it adds a [PEP566](https://peps.python.org/pep-0566/) metadata file containing information about your app, and Briefcase itself. You can retrieve this information at runtime using the Python builtin library [`importlib.metadata`][]. `importlib.metadata` was added in Python 3.8; however, it has been backported and published on PyPI as [`importlib.metadata`][] for older versions of Python. |
| 4 |
|
| 5 |
To access application metadata at runtime, you can use the following code: |
| 6 |
|
| 7 |
```python |
| 8 |
import sys |
| 9 |
try: |
| 10 |
from importlib import metadata as importlib_metadata |
| 11 |
except ImportError: |
| 12 |
# Backwards compatibility - importlib.metadata was added in Python 3.8 |
| 13 |
import importlib_metadata |
| 14 |
|
| 15 |
# Find the name of the module that was used to start the app |
| 16 |
app_module = sys.modules['__main__'].__package__ |
| 17 |
# Retrieve the app's metadata |
| 18 |
metadata = importlib_metadata.metadata(app_module) |
| 19 |
``` |
| 20 |
|
| 21 |
The `metadata` returned by this code will be a dictionary-like object that contains the following identifying keys: |
| 22 |
|
| 23 |
- **Metadata-Version** - The syntax version of the metadata file itself (as defined in [PEP566](https://peps.python.org/pep-0566/)). |
| 24 |
- **Briefcase-Version** - The version of Briefcase used to package the app. The existence of this key in app metadata can be used to identify if your application code is running in a Briefcase container; it will only exist if the app has been packaged by Briefcase. |
| 25 |
|
| 26 |
It will also have the following keys, derived from your application's `pyproject.toml` configuration: |
| 27 |
|
| 28 |
- **Name** - [`app_name`][] |
| 29 |
- **Formal-Name** - [`formal_name`][] |
| 30 |
- **App-ID** - [`bundle`][] and [`app_name`][], joined with a `.` |
| 31 |
- **Version** - [`version`][] |
| 32 |
- **Summary** - [`description`][] |
| 33 |
|
| 34 |
The metadata may also contain the following keys, if they have been defined in your app's `pyproject.toml` configuration: |
| 35 |
|
| 36 |
- **Home-page** - [`url`][] |
| 37 |
- **Author** - [`author`][] |
| 38 |
- **Author-email** - [`author_email`][] |
| 39 |
|
| 40 |
For example, the metadata for the app constructed by the [BeeWare Tutorial](https://tutorial.beeware.org/en/latest/tutorial/tutorial-1) would contain: |
| 41 |
|
| 42 |
```python |
| 43 |
Metadata-Version: 2.1 |
| 44 |
Briefcase-Version: 0.3.1 |
| 45 |
Name: helloworld |
| 46 |
Formal-Name: Hello World |
| 47 |
App-ID: com.example.helloworld |
| 48 |
Version: 0.0.1 |
| 49 |
Home-page: https://example.com/helloworld |
| 50 |
Author: Jane Developer |
| 51 |
Author-email: [email protected] |
| 52 |
Summary: My first application |
| 53 |
``` |