Python falls quick in a number of spots. For occasion, Python isn’t the fastest language around, but third-party libraries like NumPy permit you function around that. Wherever Python is most deficient, while, is packaging. That is, Python lacks a steady inside system for making a standalone binary from an software. Go and Rust do this. Why can not Python?
It mainly comes down to Python not owning a tradition of these types of use scenarios till comparatively lately in its heritage. And so, only comparatively lately did third-party modules start out demonstrating up that allow for Python applications to be packaged as standalone binaries. PyInstaller — which I included earlier — is 1 these types of application. In this posting we’ll search at an even a lot more elegant and effective utility for Python application packaging, BeeWare’s Briefcase.
[ Also on InfoWorld: Python virtualenv and venv do’s and don’ts ]
Having said that, there are two caveats well worth pointing out about Briefcase. First, Briefcase doesn’t do cross-system packaging you need to build on the system you are deploying for. Second, Briefcase performs most effective with applications that make use of a GUI toolkit of some form. We’ll go into depth about these issues below.
What is BeeWare Briefcase?
Briefcase is part of a general suite of equipment by BeeWare for creating applications, with the distinctive parts complementing every single other. For occasion, BeeWare’s Kivy allows you create cross-system GUI applications in Python that operate not only on all the important OS platforms but also on the web. But in this article we’ll focus on Briefcase, which can be utilized with or without the other equipment.
Briefcase packages applications for all the OSes it supports by way of a frequent format for applications on that system:
- Microsoft Windows (MSI installer)
- macOS (
- Linux (AppImage)
- iOS (Xcode undertaking)
- Android (Gradle undertaking)
To deploy on iOS or Android, you’ll need the advancement kits for people platforms.
A person issue Briefcase does not support is cross-system deployment. For occasion, if you are a Windows person, you can not build a macOS application you’ll need macOS to do that. Other application bundlers for Python are in the same way constrained, so this restriction is by no usually means exceptional to Briefcase.
Briefcase is also not a “compiler” — it doesn’t remodel Python plans into their indigenous device-code equivalents. Your applications won’t operate any faster when deployed as Briefcase applications than they do typically.
Briefcase undertaking set up
Briefcase involves you to set up a committed undertaking directory with its own digital surroundings. If you are not familiar with “venvs” but, as Python digital environments are called, it’s well worth receiving up to speed on them, as condition-of-the-art Python advancement revolves intensely around them.
Soon after you set up a venv and
pip install briefcase into it, you’ll use Briefcase’s personal command-line tooling to set up, control, and provide Briefcase-packaged tasks. This is akin to the way equipment like Poetry work: Most of your higher-degree interactions with the undertaking are by the software, so you really do not have to manually create data files or edit configurations.
To kick off a new Briefcase undertaking, open up the CLI in your undertaking directory, activate the digital surroundings (assuming you are not using an IDE’s CLI to do that instantly), and type
briefcase new. This results in scaffolding in your undertaking directory for a Briefcase undertaking.
You are going to need to answer some issues about the undertaking at very first, and for most of them you can just press
Enter to take the default. But 1 of the issues you’ll be asked — the past 1, in reality — matters tremendously: the preference of GUI framework to use.
A person of BeeWare’s other choices is a UI toolkit called Toga, for creating GUIs in Python plans using system-indigenous UI elements. If you want to soar into studying Toga whilst also performing with Briefcase, there is very little stopping you. Or you could select “None” and create a “headless” application that runs from the command line, or you could use a third-party UI toolkit or windowing method these types of as Pyglet or PyQT.
Be aware that if you install no UI toolkit, the application will have no console interactivity in any way — i.e., it won’t open up a console window and it won’t print anything at all to the console. This is useful if you are deploying a software that doesn’t demand console conversation — for occasion, if it runs as a regional web server and makes use of a web browser for conversation. But there is as of but no possibility to allow for Briefcase plans with no UI offer installed to operate with a console.
Briefcase undertaking composition
A freshly initiated Briefcase application directory comes with several data files pre-installed:
- The leading degree of the application directory incorporates the project’s license,
pyproject.tomlfile, a sample README file in ReStructured Textual content format, and a
.gitignorefile that comes pre-tailored with frequent directories to omit from any Git repository established for the undertaking.
srcdirectory incorporates the supply code of your application, with two subdirectories: 1 that incorporates the application (it has the exact name as your undertaking directory) and 1 that incorporates the app’s metadata.
- The application directory incorporates a
sourcesdirectory, which is utilized to retailer sources like software icons.
Briefcase undertaking instructions
briefcase command is how you complete most of your interactions with a Briefcase undertaking. We included the
new command over, which is utilized to set up a Briefcase undertaking in a supplied folder. But you’ll usually need to use numerous other instructions for the duration of the lifecycle of a Briefcase application, and some of them can be a minor counterintuitive.
Here are the most frequent Briefcase instructions you’ll use:
dev: When you are within an application directory, this command runs that application in dev manner. Dev manner allows you operate the software with its full enhance of installed libraries, but without needing to be formally packaged for supply. Most of the time, when developing your software, you’ll test-operate it with dev manner. If any dependencies have transformed since the past time you ran
dev, use the
-dflag to update them.
build: Builds a copy of the software in the variety needed to offer it for distribution. This differs from
devin that you can build for distinctive platforms if the scaffolding is installed.
update: Updates an software build. This is the fast way to make absolutely sure the build of your software has the most the latest code, somewhat than using
build, which regenerates numerous a lot more data files. Move the
-dflag to update dependencies, and the
-rflag to update sources (that is, to copy sources from the dev edition of your application to the build edition).
operate: Operates the crafted edition of the application. This basically simulates jogging the packaged and deployed edition of the software. Move the
-uflag to update any code just before jogging.
offer: Generates an software installer offer from the crafted edition of the application. The conclude outcome of this is an artifact you can give to some others to install your software — e.g., an .MSI on Windows.
Here are some of the a lot less typically utilized Briefcase instructions:
create: Not to be bewildered with
createcreates the scaffolding for an software installer — a way to build the app’s installer for a distinct system. When you set up an application with
new, it comes with scaffolding for the system you are performing on
createlets you include scaffolding for one more system if needed.
up grade: Upgrades the elements utilized to offer the application, these types of as the Wix framework.
publish: Publishes the packaged application to a publication channel these types of as an application retailer. (As of this writing, this feature doesn’t function but.)
To sum up, this is the order in which you would use the Briefcase instructions in the standard application lifecycle:
newto create the application
devto operate the application as you function on it
buildto create a edition of the application to be packaged for distribution
operateto test-operate the packaged edition of the application
updateto maintain the packaged edition of the application up-to-day with code changes
offerto deploy the packaged edition of the application with an installer
Briefcase application generation
Creating a Python software as a Briefcase application is a great deal the exact as creating any other Python application. The principal issues include the undertaking composition. The app’s entry place is
__principal__.py in the application directory, which loads
application.py from the exact directory and executes
principal(). When you initialize a undertaking, it will be populated with placeholder versions of some undertaking data files, which you can build out or replace as needed.
If you are transforming an existing project to use Briefcase, make absolutely sure you composition it in these types of a way that its entry place is what Briefcase expects. For occasion, if you did not retailer the code in a
src directory, you’ll need to move your code into
src and resolve any incompatibilities in its paths and directory structures.
The other issue to maintain in mind is how to take care of third-party dependencies. The
pyproject.toml file in your undertaking directory controls which dependencies to include to the undertaking. If your undertaking is named
pyproject.toml will consist of a section named
[software.briefcase.application.myproject], with a
involves line that lists every single prerequisite as they’d be specified in a
demands.txt file. If your undertaking requires, for occasion,
black, you would set that line to
involves = ["regex","black"]. You’d then use
briefcase dev -d to update the dependencies for the advancement edition of the undertaking, and
briefcase update -d to update dependencies in the packaged edition.
Briefcase application packaging and supply
Once you run
briefcase offer, you will see a redistributable for your software surface in a subdirectory of the undertaking directory that corresponds to the system you crafted for. For Microsoft Windows, for occasion, the directory will be
home windows, and the redistributable will be an
.msi file with the exact name as your undertaking. For Android and iOS, the success will be tasks for Gradle and Xcode, respectively, and these will need to be compiled using people equipment to be deployable to people platforms.
How to do a lot more with Python
Copyright © 2020 IDG Communications, Inc.