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.
helloworld directory, run:
You’ve probably just seen pages of content go past in your terminal… so what just happened? Briefcase has done the following:
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, subsituting 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.
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 innefficient - 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.
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.
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.
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 (
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.
Running your app¶
You can now use Briefcase to run your application:
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 also 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.