desktop-lifecycle
The boring parts of a small Python desktop app — before you write any UI.
Single-instance lock, XDG directories, autostart, menu entry, session detection, tray capability and rotating logs. No framework, no dependencies, no root.
pip install desktop-lifecycle
from desktop_lifecycle import App
app = App("my-tool").ensure_dirs()
app.setup_logging()
if not app.acquire(): # a second copy was launched
raise SystemExit(0) # it already asked the first one to show itself
That is the whole idea: the four things every small desktop app gets wrong, in one small library you can read in ten minutes.
Why this exists
Every Python utility that lives in the tray ends up re-implementing the same six things, usually differently and often incorrectly:
| What you need | What usually happens instead |
|---|---|
| One process per session | a PID file that lies after a crash, because PIDs are reused |
| Somewhere to write | settings in ~/.config, logs in the checkout, a state file at /.local/state |
| Start at login | three platform-specific files written by hand |
| A menu entry | an unescaped Exec= line that silently refuses to launch |
| Know the session | DISPLAY is set, so it must be X11 — except under XWayland it is not |
| A tray icon | pystray works on X11, does not on Wayland, and needs PyGObject built |
These are not interesting problems, but each one produces a bug report that
takes an evening to understand. desktop-lifecycle is the version that was
already debugged.
What you get
| Module | What it does |
|---|---|
App |
ties everything below together in one object |
Paths |
~/.config, ~/.local/state, ~/.local/share, ~/.cache (and the Windows/macOS equivalents), created with 0700 |
SingleInstance |
advisory flock / msvcrt lock, plus a signal file so a second launch can raise the existing window |
Autostart |
XDG autostart entry, per-user Windows Run key, macOS LaunchAgent — user-scoped, reversible |
install_menu_entry |
spec-correct .desktop file with properly escaped Exec= and atomic writes |
session_type |
x11, wayland, windows, macos, unknown — from XDG_SESSION_TYPE, not from DISPLAY |
tray.backend |
answers which tray backend will appear: ayatana, appindicator, pystray or none |
setup_logging |
rotating log in the state directory, so users can find it |
More detail, including package names per distribution: docs/tray-on-wayland.md.
A complete tray application
See examples/tk_demo.py — about sixty lines, and it
already handles the second launch, the tray, the hide-to-tray behaviour and
the log file. Run it twice to see the hand-over.
What this is not
- Not a GUI toolkit. It does not draw anything. Tkinter, PyQt, GTK and wxPython all work with it.
- Not a tray replacement. It detects the right backend and builds a small menu, but it will not pretend a tray exists where the desktop has none.
- Not a daemon framework. It tells you where the lock and log live; how your process loops is your business.
Platform support
| Linux X11 | Linux Wayland | Windows | macOS | |
|---|---|---|---|---|
| Paths | ✅ | ✅ | ✅ | ✅ |
| Single instance | ✅ | ✅ | ✅ | ✅ |
| Autostart | ✅ XDG | ✅ XDG | ✅ HKCU\...\Run |
✅ LaunchAgent |
| Menu entry | ✅ | ✅ | — | — |
| Session detection | ✅ | ✅ | ✅ | ✅ |
| Tray | ✅ AppIndicator | ✅ AppIndicator | ✅ pystray | ✅ pystray |
Tray icons on Wayland only exist through the AppIndicator / StatusNotifierItem
protocol. Tray icons additionally need a StatusNotifierItem host: GNOME ships one only with the AppIndicator extension enabled. tray.backend() checks for it and answers none when it is missing. If the answer is none, your app must keep
working with a visible window — that is exactly the case this library
forces you to handle.
Security notes
- Everything is user-scoped. Nothing here asks for root, writes outside your home directory, or edits system configuration.
- Lock files are created
0600; configuration directories0700; desktop entries0644. - Every file is written atomically (temporary file +
rename) so a crash cannot leave half an autostart entry behind. Exec=values are escaped the way the specification requires —",`,$,\and%cannot break out of the quoted argument or become a field code.- No
shell=Trueanywhere, noeval, no dynamic imports from user input. - Application names are validated, so a name cannot escape its own directory.
- No network access at runtime, no telemetry, no accounts.
Found a problem? See SECURITY.md.
Checking a machine
python -m desktop_lifecycle # human-readable
python -m desktop_lifecycle --json # for bug reports
It prints the resolved directories, the session type and the tray backend
without writing anything. Users can paste that into an issue instead of
ls -la ~.
Compared to the alternatives
| desktop-lifecycle | pystray | manual code | |
|---|---|---|---|
| XDG paths | ✅ | — | usually wrong |
| Single-instance lock | ✅ | — | PID file |
| Autostart | ✅ | — | per project |
| Menu entry | ✅ | — | per project |
| Wayland-aware tray detection | ✅ | partial | rare |
| GUI toolkit required | no | no | no |
| Dependencies | none | Pillow, PyGObject | — |
pystray is good at what it does; this library answers the questions around
it, including "should I even try".
Requirements
Python 3.9+. No third-party packages.
Optional tray fallback: pip install desktop-lifecycle[tray] (pystray + Pillow).
On Debian/Ubuntu/Mint the native way is gir1.2-ayatanaappindicator3-0.1.
Development
git clone https://github.com/1emrecoskun/desktop-lifecycle
cd desktop-lifecycle
python -m unittest discover -s tests -v
79 tests, no hardware and no display required.
Licence, names and data
The MIT licence covers the code. It disclaims warranty and liability, which is the honest position for a small library maintained in public. It does not grant trademark rights, so the project name and icon stay with the project.
The library collects nothing: no telemetry, no account, no network request, no analytics. An application using it still has to describe its own data handling, because it is the one with the window.
MIT — see LICENSE.
Built by Emre Coşkun, who also maintains ble-led-sync, the application this library was extracted from.
Türkçe: README.tr.md
Metadata
Release files for desktop-lifecycle 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| desktop_lifecycle-0.1.4.tar.gz | 40.7 kB | With provenance | Details for desktop_lifecycle-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| desktop_lifecycle-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for desktop_lifecycle-0.1.4-py3-none-any.whl |
Total release size: 65.8 kB
Release files / desktop_lifecycle-0.1.4.tar.gz
| Download URL | desktop_lifecycle-0.1.4.tar.gz |
|---|---|
| Size | 40.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a03f20f3c6a7c48c30aab111a90fb15477acb6b4f8fc18a3d43c4c9d7ef9136b
|
|
BLAKE2b-256 checksum How to use checksums |
b570d1b83e594e60b2b75213390fda125ef056ca252b47f17843294d076e35b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.
Transparency logRelease files / desktop_lifecycle-0.1.4-py3-none-any.whl
| Download URL | desktop_lifecycle-0.1.4-py3-none-any.whl |
|---|---|
| Size | 25.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
81146bab2e713094e8a720413ac387dad5b249dae8b45debfb93a97e1695a5c2
|
|
BLAKE2b-256 checksum How to use checksums |
c3e6b5ca736f52796e0396a7f1b10758f513ca095523d0a7061c7ab16a8ba9e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.
Transparency logSecurity for desktop-lifecycle 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for desktop_lifecycle-0.1.4.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.