Python apps


#1

Python has Setuptools, and the Python Package Index, to assist in packaging and distributing applications to other developers. And Virtualenv allows developers to isolate an application and its dependencies from the rest of the system. However, pip and virtualenv are not user-oriented tools. Nor do they offer a solution for notifying users of available updates.

Snaps address these gaps, while building upon the work you’ve already done to teach Python how to package your app.

What benefits do snaps bring to Python apps?

  • Snaps are easy to discover and install. Millions of users can browse and install snaps graphically in the Snap Store or from the command-line.
  • Snaps install and run the same across Linux. They bundle the exact version of Python required and all of your app’s dependencies, be they Python modules or system libraries.
  • Snaps automatically update to the latest version. Four times a day, users’ systems will check for new versions and upgrade in the background.
  • Upgrades are not disruptive. Because upgrades are not in-place, users can keep your app open as it’s upgraded in the background.
  • Upgrades are safe. If your app fails to upgrade, users automatically roll back to the previous revision.

Do you have twenty minutes to get started?

Ready to get started? By the end of this guide you’ll understand how to make a snap of your Python app that can be published in the Snap Store, showcasing it to millions of Linux users.

Diving in

Snaps are defined in a single YAML file placed in the root of your project. The offlineimap example shows the entire snapcraft.yaml file for an existing project. Don’t worry, we’ll break this down.

name: offlineimap
version: git
summary: OfflineIMAP
description: >
  OfflineIMAP is software that downloads your email mailbox(es) as local
  Maildirs. OfflineIMAP will synchronize both sides via IMAP.

confinement: devmode

parts:
  offlineimap:
    plugin: python
    python-version: python2
    source: .

apps:
  offlineimap:
    command: bin/offlineimap

Metadata

The snapcraft.yaml file starts with a small amount of human-readable metadata, which usually can be lifted from the GitHub description or project README.md. This data is used in the presentation of your app in the Snap Store.

name: offlineimap
version: git
summary: OfflineIMAP
description: >
  OfflineIMAP is software that downloads your email mailbox(es) as local
  Maildirs. OfflineIMAP will synchronize both sides via IMAP.

The name must be unique in the Snap Store. Valid snap names consist of lower-case alphanumeric characters and hyphens. They cannot be all numbers. They also cannot start or end with a hyphen.

By specifying git for the version, the current git tag or commit will be used as the version string. Versions carry no semantic meaning in snaps.

The summary can not exceed 79 characters. You can use a chevron ‘>’ in the description key to declare a multi-line description.

Security model

The next section describes the level of confinement applied to your app.

confinement: devmode

Snaps are containerised to ensure more predictable application behaviour and greater security. Unlike other container systems, the shape of this confinement can be changed through a set of interfaces. These are declarations that tell the system to give permission for a specific task, such as accessing a webcam or binding to a network port.

It’s best to start a snap with the confinement in warning mode, rather than strictly applied. This is indicated through the devmode keyword. When a snap is in devmode, runtime confinement violations will be allowed but reported. These can be reviewed by running journalctl -xe.

Because devmode is only intended for development, snaps must be set to strict confinement before they can be published as “stable” in the Snap Store. Once an app is working well in devmode, you can review confinement violations, add appropriate interfaces, and switch to strict confinement.

Parts

Parts define what sources are needed to assemble your app. Parts can be anything: programs, libraries, or other needed assets. We’ll deal with libraries and other assets later, so for now we just have one part: the offlineimap source code.

parts:
  offlineimap:
    plugin: python
    python-version: python2
    source: .

The python plugin builds upon the work you’ve already done to describe your Python dependencies in setup.py and requirements.txt. It will automatically include these in your snap.

The python-version keyword instructs the python plugin to include a copy of Python 2 in your snap. If unspecified, this option defaults to Python 3.

The source keyword points to the root of your Python project and can be a local directory or remote Git repository. Note that your Python project should be using setuptools and You should be able to run python setup.py bdist_wheel without errors. If either of these are not true, please consult the setuptools documentation.

Apps

Apps are the commands you want to expose to users and any background services your application provides. Each key under apps is the command name that should be made available on users’ systems.

The command specifies the path to the binary to be run. This is resolved relative to the root of your snap contents and automatically searches in the usr/sbin, usr/bin, sbin, and bin subdirectories of your snap.

apps:
  offlineimap:
    command: bin/offlineimap

If your command name matches the snap name, users will be able run the command directly. If the names differ, then apps are prefixed with the snap name (offlineimap.command-name, for example). This is to avoid conflicting with apps defined by other installed snaps.

You can request an alias on the Snapcraft forum if your command name and snap name do not match but you don’t want your command prefixed. These aliases are set up automatically when your snap is installed from the Snap Store.

Further customisations

Here are all the Python plugin-specific keywords:

- requirements:
  (string)
  Path to a requirements.txt file
- constraints:
  (string)
  Path to a constraints file
- process-dependency-links:
  (bool; default: false)
  Enable the processing of dependency links in pip, which allow one project
  to provide places to look for another project
- python-packages:
  (list)
  A list of dependencies to get from PyPI
- python-version:
  (string; default: python3)
  The python version to use. Valid options are: python2 and python3

Next steps

That’s it. You now have a snapcraft.yaml file which describes how the core of your app is assembled, presented, and run.

Continue on to learn how you can bundle your app’s dependencies, such as system libraries, into this snap so your app is portable across Linux distributions.


Creating a snap
The python plugin
Snapcraft overview
Proposed new documentation outline
#2

@evan is there a reason this isn’t a wiki? I believe the intent of posts in this category to be wikis to facilitate crowd-sourcing.


#3

Fixed, it’s now a wiki :slight_smile:


#4

We should include a prerequisite state of your project section at the beginning of this:

“You should be using setuptools in your project. You should be able to run python setup.py bdist_wheel without errors. If either of these are not true, please consult the setuptools documentation.”


#5

@evan https://www.youtube.com/watch?v=G2y8Sx4B2Sk

It’s setuptools not setuputils


#6

lol, and I’m not sure why it’s incorrectly wedged in my brain given no shortage of personal projects using it. Thanks, fixed.


#7

@evan @degville Many Python applications require UTF-8 support but this requires workarounds due to the longstanding issue of poor locale support in snapd.

Many people encounter this issue and cook up their own solution to it so it might be best to document it and provide a best-practice solution.

The least intrusive and most generic solution is to add the following bits to the command part:

      LC_ALL: "C.UTF-8"
      LANG: "C.UTF-8"

This also works for non-python applications.

Where should I document this?


#8

Also, a few more comments on this page:

It’s best to start a snap with the confinement in warning mode, rather than strictly applied. This is indicated through the devmode keyword. When a snap is in devmode, runtime confinement violations will be allowed but reported. These can be reviewed by running journalctl -xe.

  1. What is “warning mode”? Is this “devmode” confinement?
  2. It would be ideal if this part contained a link to more in-depth documentation on confinement.

It’s also useful to point to more resources about debugging the build on this page, like the “iterating without rebuilding” part of debugging: https://docs.snapcraft.io/debugging-building-snaps/6274


#9

This is a great guide, but I recommend linking to somewhere next at the bottom of the page. As someone reading the document, it feels slightly disorientating to be told to “Continue”, but having no indication as to where the content is that is being referred to.

Next Steps

Continue on to learn how you can bundle your app’s dependencies, such as system libraries, into this snap so your app is portable across Linux distributions.


#10

Which effectively disables the I18N. I have written a launcher remote part to solve this issue at my end: The `locales-launch` Remote Part


#11

I confirm this fixes the issue I mentioned, with the added benefit that localization now actually works!