How to package Python apps with BeeWare Briefcase
Python falls short in a couple of areas. For instance, Python isn’t the quickest language all around, but 3rd-social gathering libraries like NumPy let you operate all around that. Exactly where Python is most deficient, nevertheless, is packaging. That is, Python lacks a regular inner mechanism for creating a standalone binary from an software. Go and Rust do this. Why just cannot Python?
It largely arrives down to Python not having a lifestyle of these use situations until finally reasonably not too long ago in its history. And so, only reasonably not too long ago did 3rd-social gathering modules start off showing up that enable Python applications to be packaged as standalone binaries. PyInstaller — which I covered beforehand — is one these app. In this short article we’ll seem at an even a lot more exquisite and strong utility for Python app packaging, BeeWare’s Briefcase.
[ Also on InfoWorld: Python virtualenv and venv do’s and don’ts ]
However, there are two caveats truly worth pointing out about Briefcase. Very first, Briefcase doesn’t do cross-platform packaging you will need to construct on the platform you’re deploying for. Next, Briefcase performs greatest with applications that make use of a GUI toolkit of some sort. We’ll go into detail about these concerns underneath.
What is BeeWare Briefcase?
Briefcase is component of a standard suite of instruments by BeeWare for making applications, with the distinct parts complementing each individual other. For instance, BeeWare’s Kivy lets you build cross-platform GUI applications in Python that operate not only on all the main OS platforms but also on the world wide web. But here we’ll target on Briefcase, which can be employed with or with out the other instruments.
Briefcase deals applications for all the OSes it supports by way of a popular structure for applications on that platform:
- Microsoft Windows (MSI installer)
- macOS (
.app
format file) - Linux (AppImage)
- iOS (Xcode venture)
- Android (Gradle venture)
To deploy on iOS or Android, you are going to will need the enhancement kits for individuals platforms.
One particular thing Briefcase does not support is cross-platform deployment. For instance, if you’re a Windows person, you just cannot construct a macOS app you are going to will need macOS to do that. Other app bundlers for Python are likewise constrained, so this restriction is by no means exclusive to Briefcase.
Briefcase is also not a “compiler” — it doesn’t remodel Python programs into their native machine-code equivalents. Your applications will not operate any more rapidly when deployed as Briefcase applications than they do typically.
Briefcase venture set up
Briefcase involves you to set up a focused venture directory with its own virtual surroundings. If you’re not common with “venvs” nevertheless, as Python virtual environments are referred to as, it’s truly worth receiving up to pace on them, as condition-of-the-artwork Python enhancement revolves heavily all around them.
Immediately after you set up a venv and pip set up briefcase
into it, you are going to use Briefcase’s individual command-line tooling to set up, take care of, and produce Briefcase-packaged jobs. This is akin to the way instruments like Poetry work: Most of your higher-level interactions with the venture are by means of the software, so you never have to manually build files or edit configurations.
To kick off a new Briefcase venture, open up the CLI in your venture directory, activate the virtual surroundings (assuming you’re not employing an IDE’s CLI to do that instantly), and type briefcase new
. This produces scaffolding in your venture directory for a Briefcase venture.
You will will need to answer some thoughts about the venture at to start with, and for most of them you can just press Enter
to accept the default. But one of the thoughts you are going to be questioned — the final one, in actuality — matters considerably: the alternative of GUI framework to use.
One particular of BeeWare’s other choices is a UI toolkit referred to as Toga, for making GUIs in Python programs employing platform-native UI components. If you want to soar into mastering Toga whilst also doing work with Briefcase, there’s practically nothing halting you. Or you could select “None” and build a “headless” app that runs from the command line, or you could use a 3rd-social gathering UI toolkit or windowing program these as Pyglet or PyQT.
Notice that if you set up no UI toolkit, the app will have no console interactivity in any respect — i.e., it will not open up a console window and it will not print something to the console. This is valuable if you’re deploying a plan that doesn’t involve console interaction — for instance, if it runs as a community world wide web server and takes advantage of a world wide web browser for interaction. But there is as of nevertheless no selection to enable Briefcase programs with no UI package deal installed to operate with a console.
Briefcase venture composition
A freshly initiated Briefcase app directory arrives with quite a few files pre-installed:
- The prime level of the app directory contains the project’s license,
pyproject.toml
file, a sample README file in ReStructured Text structure, and a.gitignore
file that arrives pre-personalized with popular directories to omit from any Git repository designed for the venture. - The
src
directory contains the supply code of your app, with two subdirectories: one that contains the app (it has the identical name as your venture directory) and one that contains the app’s metadata. - The app directory contains a
assets
directory, which is employed to retail outlet assets like software icons.
Briefcase venture instructions
The briefcase
command is how you execute most of your interactions with a Briefcase venture. We covered the new
command above, which is employed to set up a Briefcase venture in a provided folder. But you are going to ordinarily will need to use several other instructions during the lifecycle of a Briefcase app, and some of them can be a minimal counterintuitive.
Listed here are the most popular Briefcase instructions you are going to use:
dev
: When you’re inside an app directory, this command runs that app in dev mode. Dev mode lets you operate the software with its total enhance of installed libraries, but with out needing to be formally packaged for delivery. Most of the time, when establishing your software, you are going to test-operate it with dev mode. If any dependencies have transformed because the final time you randev
, use the-d
flag to update them.construct
: Builds a duplicate of the software in the kind desired to package deal it for distribution. This differs fromdev
in that you can construct for distinct platforms if the scaffolding is installed.update
: Updates an software construct. This is the brief way to make sure the construct of your software has the most current code, fairly than usingconstruct
, which regenerates several a lot more files. Go the-d
flag to update dependencies, and the-r
flag to update assets (that is, to duplicate assets from the dev version of your app to the construct version).operate
: Operates the built version of the app. This basically simulates functioning the packaged and deployed version of the software. Go the-u
flag to update any code in advance of functioning.package deal
: Makes an software installer package deal from the built version of the app. The conclude consequence of this is an artifact you can give to others to set up your plan — e.g., an .MSI on Windows.
Listed here are some of the less usually employed Briefcase instructions:
build
: Not to be baffled withnew
,build
creates the scaffolding for an software installer — a way to construct the app’s installer for a particular platform. When you set up an app withnew
, it arrives with scaffolding for the platform you’re doing work onbuild
lets you include scaffolding for yet another platform if desired.upgrade
: Upgrades the components employed to package deal the app, these as the Wix framework.publish
: Publishes the packaged app to a publication channel these as an app retail outlet. (As of this creating, this function doesn’t operate nevertheless.)
To sum up, this is the buy in which you would use the Briefcase instructions in the common app lifecycle:
new
to build the appdev
to operate the app as you operate on itconstruct
to build a version of the app to be packaged for distributionoperate
to test-operate the packaged version of the appupdate
to hold the packaged version of the app up-to-day with code adjustmentspackage deal
to deploy the packaged version of the app with an installer
Briefcase app generation
Producing a Python plan as a Briefcase app is significantly the identical as making any other Python app. The principal concerns include the venture composition. The app’s entry point is __principal__.py
in the app directory, which loads app.py
from the identical directory and executes principal()
. When you initialize a venture, it will be populated with placeholder variations of some venture files, which you can construct out or replace as desired.
If you’re reworking an existing project to use Briefcase, make sure you composition it in these a way that its entry point is what Briefcase expects. For instance, if you did not retail outlet the code in a src
directory, you are going to will need to transfer your code into src
and correct any incompatibilities in its paths and directory buildings.
The other thing to hold in thoughts is how to take care of 3rd-social gathering dependencies. The pyproject.toml
file in your venture directory controls which dependencies to include to the venture. If your venture is named myproject
, then pyproject.toml
will comprise a area named [software.briefcase.app.myproject]
, with a involves
line that lists each individual necessity as they’d be specified in a prerequisites.txt
file. If your venture requires, for instance, regex
and black
, you would set that line to involves = ["regex","black"]
. You’d then use briefcase dev -d
to update the dependencies for the enhancement version of the venture, and briefcase update -d
to update dependencies in the packaged version.
Briefcase app packaging and delivery
After you run briefcase package deal
, you will see a redistributable for your plan seem in a subdirectory of the venture directory that corresponds to the platform you built for. For Microsoft Windows, for instance, the directory will be home windows
, and the redistributable will be an .msi
file with the identical name as your venture. For Android and iOS, the success will be jobs for Gradle and Xcode, respectively, and these will will need to be compiled employing individuals instruments to be deployable to individuals platforms.
How to do a lot more with Python
Copyright © 2020 IDG Communications, Inc.