def ensure_app(headless=True): global _store_app with _ea_lock: if _store_app is None and QApplication.instance() is None: args = sys.argv[:1] if headless and (islinux or isbsd): args += ['-platformpluginpath', sys.extensions_location, '-platform', 'headless'] _store_app = QApplication(args) if headless and (islinux or isbsd): _store_app.headless = True import traceback # This is needed because as of PyQt 5.4 if sys.execpthook == # sys.__excepthook__ PyQt will abort the application on an # unhandled python exception in a slot or virtual method. Since ensure_app() # is used in worker processes for background work like rendering html # or running a headless browser, we circumvent this as I really # dont feel like going through all the code and making sure no # unhandled exceptions ever occur. All the actual GUI apps already # override sys.except_hook with a proper error handler. def eh(t, v, tb): try: traceback.print_exception(t, v, tb, file=sys.stderr) except: pass sys.excepthook = eh