Tutorial 3 - Packaging for distribution

So far, we’ve been running our application in “Developer mode”. This makes it easy for us to run our application locally - but what we really want is to be able to give our application to others.

However, we don’t want to have to teach our users how to install Python, create a virtual environment, clone a git repository, and run Briefcase in developer mode. We’d rather just give them an installer, and have the application Just Work.

Briefcase can be used to package your application for distribution in this way.

Creating your application scaffold

Since this is the first time we’re packaging our application, we need to create some configuration files and other scaffolding to support the packaging process. From the helloworld directory, run:

(beeware-venv) $ briefcase create

[helloworld] Generating application template...
Using app template: https://github.com/beeware/briefcase-macOS-app-template.git, branch v0.3.18
...

[helloworld] Installing support package...
...

[helloworld] Installing application code...
Installing src/helloworld... done

[helloworld] Installing requirements...
...

[helloworld] Installing application resources...
...

[helloworld] Removing unneeded app content...
Removing unneeded app bundle content... done

[helloworld] Created build/helloworld/macos/app

You’ve probably just seen pages of content go past in your terminal… so what just happened? Briefcase has done the following:

  1. It generated an application template. There’s a lot of files and configurations required to build a native installer, above and beyond the code of your actual application. This extra scaffolding is almost the same for every application on the same platform, except for the name of the actual application being constructed - so Briefcase provides an application template for each platform it supports. This step rolls out the template, substituting the name of your application, bundle ID, and other properties of your configuration file as required to support the platform you’re building on.

    If you’re not happy with the template provided by Briefcase, you can provide your own. However, you probably don’t want to do this until you’ve got a bit more experience using Briefcase’s default template.

  2. It downloaded and installed a support package. The packaging approach taken by Briefcase is best described as “the simplest thing that could possibly work” - it ships a complete, isolated Python interpreter as part of every application it builds (except for Linux native system package builds). This is slightly space inefficient - if you have 5 applications packaged with Briefcase, you’ll have 5 copies of the Python interpreter. However, this approach guarantees that every application is completely independent, using a specific version of Python that is known to work with the application.

    Again, Briefcase provides a default support package for each platform; if you want, you can provide your own support package, and have that package included as part of the build process. You may want to do this if you have particular options in the Python interpreter that you need to have enabled, or if you want to strip modules out of the standard library that you don’t need at runtime.

    Briefcase maintains a local cache of support packages, so once you’ve downloaded a specific support package, that cached copy will be used on future builds.

    As noted above, when Briefcase packages an app as a native Linux system package (the default package format for Linux), a support package is not included with the app. Instead, the app will use the Python that is provided by the distribution of Linux being targeted.

  3. It installed application requirements. Your application can specify any third-party modules that are required at runtime. These will be installed using pip into your application’s installer.

  4. It Installed your application code. Your application will have its own code and resources (e.g., images that are needed at runtime); these files are copied into the installer.

  5. It installed your resources needed by your application. Lastly, it adds any additional resources that are needed by the installer itself. This includes things like icons that need to be attached to the final application and splash screen images.

Once this completes, if you look in the project directory, you should now see a directory corresponding to your platform (macOS, linux, or windows) that contains additional files. This is the platform-specific packaging configuration for your application.

Building your application

You can now compile your application. This step performs any binary compilation that is necessary for your application to be executable on your target platform.

(beeware-venv) $ briefcase build

[helloworld] Adhoc signing app...
...
Signing build/helloworld/macos/app/Hello World.app
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 00:07

[helloworld] Built build/helloworld/macos/app/Hello World.app

On macOS, the build command doesn’t need to compile anything, but it does need to sign the contents of binary so that it can be executed. This signature is an ad hoc signature - it will only work on your machine; if you want to distribute the application to others, you’ll need to provide a full signature.

Running your app

You can now use Briefcase to run your application:

(beeware-venv) $ briefcase run

[helloworld] Starting app...
===========================================================================
Configuring isolated Python...
Pre-initializing Python runtime...
PythonHome: /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python-stdlib
PYTHONPATH:
- /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python311.zip
- /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python-stdlib
- /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python-stdlib/lib-dynload
- /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/app_packages
- /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/app
Configure argc/argv...
Initializing Python runtime...
Installing Python NSLog handler...
Running app module: helloworld
---------------------------------------------------------------------------

This will start to run your native application, using the app bundle created by the build command.

You might notice some small differences in the way your application looks when it’s running. For example, icons and the name displayed by the operating system may be slightly different to those you saw when running under developer mode. This is also because you’re using the packaged application, not just running Python code. From the operating system’s perspective, you’re now running “an app”, not “a Python program”, and this is reflected in how the application appears.

Building your installer

You can now package your application for distribution, using the package command. The package command does any compilation that is required to convert the scaffolded project into a final, distributable product. Depending on the platform, this may involve compiling an installer, performing code signing, or doing other pre-distribution tasks.

(beeware-venv) $ briefcase package --adhoc-sign

[helloworld] Signing app...

*************************************************************************
** WARNING: Signing with an ad-hoc identity                            **
*************************************************************************

    This app is being signed with an ad-hoc identity. The resulting
    app will run on this computer, but will not run on anyone else's
    computer.

    To generate an app that can be distributed to others, you must
    obtain an application distribution certificate from Apple, and
    select the developer identity associated with that certificate
    when running 'briefcase package'.

*************************************************************************

Signing app with ad-hoc identity...
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 00:07

[helloworld] Building DMG...
Building dist/Hello World-0.0.1.dmg

[helloworld] Packaged dist/Hello World-0.0.1.dmg

The dist folder will contain a file named Hello World-0.0.1.dmg. If you locate this file in the Finder, and double click on its icon, you’ll mount the DMG, giving you a copy of the Hello World app, and a link to your Applications folder for easy installation. Drag the app file into Applications, and you’ve installed your application. Send the DMG file to a friend, and they should be able to do the same.

In this example, we’ve used the --adhoc-sign option - that is, we’re signing our application with ad hoc credentials - temporary credentials that will only work on your machine. We’ve done this to keep the tutorial simple. Setting up code signing identities is a little fiddly, and they’re only required if you’re intending to distribute your application to others. If we were publishing a real application for others to use, we would need to specify real credentials.

When you’re ready to publish a real application, check out the Briefcase How-To guide on Setting up a macOS code signing identity.

Next steps

We now have our application packaged for distribution on desktop platforms. But what happens when we need to update the code in our application? How do we get those updates into our packaged application? Turn to Tutorial 4 to find out…