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 confguration 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
[helloworld] Installing support package...
[helloworld] Installing dependencies...
[helloworld] Installing application code...
[helloworld] Installing application resources...
[helloworld] Created macOS/Hello World

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. 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.

  3. It installed application dependencies. 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] Built macOS/Hello World/Hello World.app

On macOS, the build command doesn’t need to do anything. A .app folder is a layout convention of macOS itself; as long as the folder has a .app extension, and adheres to some internal layout rules, and provides some metadata in a known location, the folder will appear to the operating system as an application.

Running your app

You can now use Briefcase to run your application:

(beeware-venv) $ briefcase run

[helloworld] Starting app...

(beeware-venv) $

This will start to run your native application, using the output of the build command.

You’ll notice that the console output we saw earlier won’t be visible anymore. This is because we are now running a standalone, packaged app that has no (visible) console to which it can output.

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.

If you’re on macOS, you’ll also notice some small differences in the console output we saw earlier. This is because the packaged app writes its console output to the system log. When you run the packaged app, you’re seeing a filtered version of the system log, not raw console output; and as a result, there’s more system log details (like timestamps and the message source) being displayed. When you close the application, the system log will continue to run, even though there are no more logs to display. You can stop the display of the system log by typing Ctrl-C.

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 --no-sign

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

The macOS 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 --no-sign option - that is, we’ve decided to not sign our application. We’ve done this to keep the tutorial simple. Setting up code signing identities is a little fiddly, and they’re only absolutely required if you’re intending to distribute your application to others. If we were publishing a real application, you would leave off the --no-sign flag.

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…