Bug report #18155

ImportError: QtWebEngineWidgets must be imported before a QCoreApplication instance is created

Added by D M almost 7 years ago. Updated over 5 years ago.

Status:Feedback
Priority:Normal
Assignee:-
Category:Python plugins
Affected QGIS version:master Regression?:No
Operating System:Windows 10, macOS, Ubuntu Easy fix?:No
Pull Request or Patch supplied:No Resolution:fixed/implemented
Crashes QGIS or corrupts data:No Copied to github as #:26048

Description

A similar issue appears to have been reported -- see this: https://github.com/qutebrowser/qutebrowser/issues/1640.

Stack trace:

Couldn't load plugin 'myplugin' due to an error when calling its classFactory() method 

ImportError: QtWebEngineWidgets must be imported before a QCoreApplication instance is created 
Traceback (most recent call last):
  File "C:/PROGRA~1/QGIS2~1.99/apps/qgis-dev/./python\qgis\utils.py", line 336, in startPlugin
    plugins[packageName] = package.classFactory(iface)
  File "C:/Users/Me/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\myplugin\__init__.py", line 20, in classFactory
    from myplugin.plugin import QGISPlugin
  File "C:/PROGRA~1/QGIS2~1.99/apps/qgis-dev/./python\qgis\utils.py", line 664, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
  File "C:/Users/Me/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\myplugin\plugin\__init__.py", line 16, in 
    from myplugin.plugin.ui.dock import Dock
  File "C:/PROGRA~1/QGIS2~1.99/apps/qgis-dev/./python\qgis\utils.py", line 664, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
  File "C:/Users/Me/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\myplugin\plugin\ui\dock.py", line 6, in 
    from myplugin.plugin.ui.tab import Tab
  File "C:/PROGRA~1/QGIS2~1.99/apps/qgis-dev/./python\qgis\utils.py", line 664, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
  File "C:/Users/Me/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\myplugin\plugin\ui\tab_home.py", line 1, in 
    from PyQt5.QtWebEngineWidgets import QWebEngineView
  File "C:/PROGRA~1/QGIS2~1.99/apps/qgis-dev/./python\qgis\utils.py", line 664, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
ImportError: QtWebEngineWidgets must be imported before a QCoreApplication instance is created

Python version: 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] 
QGIS version: 2.99.0-Master Master, 2c53630747 

Python Path:
C:/Users/Me/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\gml_application_schema_toolbox\extlibs
C:/PROGRA~1/QGIS2~1.99/apps/qgis-dev/./python
C:/Users/Me/AppData/Roaming/QGIS/QGIS3\profiles\default/python
C:/Users/Me/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins
C:/PROGRA~1/QGIS2~1.99/apps/qgis-dev/./python/plugins
C:\Program Files\QGIS 2.99\bin\python36.zip
C:\PROGRA~1\QGIS2~1.99\apps\Python36\DLLs
C:\PROGRA~1\QGIS2~1.99\apps\Python36\lib
C:\Program Files\QGIS 2.99\bin
C:\PROGRA~1\QGIS2~1.99\apps\Python36
C:\PROGRA~1\QGIS2~1.99\apps\Python36\lib\site-packages
C:/Users/Me/AppData/Roaming/QGIS/QGIS3\profiles\default/python

History

#1 Updated by Salvatore Larosa over 6 years ago

I don't see nothing of related to QGIS app.
Maybe you could ask in mailing list for this specific error. Otherwise, please provide more info about it, thanks.

#2 Updated by D M over 6 years ago

I can successfully import `QtWebEngineWidgets` from PyQt5 5.9.2 (version currently used by QGIS 2c53630747) in a stand-alone Python installation. Below is the output from my Python Console in QGIS. I was able to reproduce this with QGIS nightly 2c53630747 for all supported platforms. Are you able to import `QtWebEngineWidgets`?

How to reproduce this:

Python Console 
Use iface to access QGIS API interface or Type help(iface) for more info
from PyQt5.QtWebEngineWidgets import QWebEngineView
Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS2~1.99\apps\Python36\lib\code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "C:/PROGRA~1/QGIS2~1.99/apps/qgis-dev/./python\qgis\utils.py", line 664, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
ImportError: QtWebEngineWidgets must be imported before a QCoreApplication instance is created

#3 Updated by Daniel Sánchez Pillot Gutiérrez over 6 years ago

I'm getting the same error when trying to import the QWebEngineView. For what I understand QtwebEngineWidgets can't be loaded since the QCoreApplication would be loaded with QGIS and we will always get this error. If this is the case, what would be the way to open a webpage in a plugin window that we could interact with?

Is that something that should be addressed at the app level since we are talking about a standard qt5 library that can't be loaded due to the way the plugin system works? or is this something that should be worked out by at he qt5 level or individually by the plugin developers?

I will continue investigating, if anyone knows of a way around this, any directions are greatly appreciated.

#4 Updated by Nyall Dawson over 6 years ago

Use QWebPage/QWebFrame/QWebView instead of QWebEngine

#5 Updated by Daniel Sánchez Pillot Gutiérrez over 6 years ago

Nyall Dawson wrote:

Use QWebPage/QWebFrame/QWebView instead of QWebEngine

Thank you Nyall, guess I reported too soon, indeed QWebView still works just needs to be loaded from QT5 (from PyQt5.QtWebKitWidgets import QWebView). I had trouble loading QWebSettings and saw suggestions to use QWebEngine instead so I tried that, but the old webkit still works so no need to worry about it yet, though there seems to be plans of deprecating the webkit so in the future this issue may have to be addressed, in the meantime, I got my old functionality back on QGIS 3. Thanks again!

#6 Updated by Nyall Dawson over 6 years ago

  • Resolution set to fixed/implemented
  • Status changed from Open to Closed

Actually qt upstream backtracked on the decision to deprecate the WebKit classes (hooray!), which is a great thing because the web engine ones are really quite limited.

#7 Updated by simone massaro over 6 years ago

Well, I am using Qt 5.9 and I cannot find QtWebKit and I only found that it was deprecated. So I am stuck on this error "QtWebEngineWidgets must be imported before a QCoreApplication instance is created".

#8 Updated by D M over 6 years ago

  • Status changed from Closed to Reopened

@ Nyall Dawson. Why do you say that the QtWebEngineWidgets is limited in comparison to WebKit?
WebKit is really quite old and I am experiencing a number of issues with it such as opening local links and rendering custom fonts.
I have reopened this for now. I have not got enough time at the moment to fix this. Is there anyone who can spare some time fixing this?
Some info: https://github.com/qutebrowser/qutebrowser/issues/1640.

#9 Updated by Nyall Dawson over 6 years ago

If it is the same issue as reported in qutebrowser, then it's a packaging issue and need reporting and fixing in osgeo4w. But if the error message is correct, and the import needs to come before creating the QCoreApplication then unfortunately there's no way we can possibly fix this in qgis.

You could test by trying the import in osgeo4w WITHOUT any qgis libraries involved.

#10 Updated by M R over 6 years ago

Nyall Dawson wrote:

You could test by trying the import in osgeo4w WITHOUT any qgis libraries involved.

I opened the osgeo4w shell on Windows and ran:

C:\>py3_env
C:\>qt5_env
C:\>python
>>> from PyQt5.QtWebEngineWidgets import QWebEngineView

This works without problem. So the error message seems to be correct and the import has to come before creating QCoreApplication. Can you shed some light on why this is hard to implement at the moment in QGIS?

#11 Updated by Nyall Dawson over 6 years ago

Can you shed some light on why this is hard to implement at the moment in QGIS?

A purely PyQt approach won't work here, because the QApplication is created in c++ code.

I think the only fix here is to call QtWebEngine::initialize() before creating the main QgsApplication. (This is the equivalent of importing QtWebEngineWidgets before creating the QCoreApplication in PyQt). But to do that, QtWebEngine would have to become a dependency of QGIS, which it currently isn't.

That's also an argument why QtWebEngine should not currently be used by plugins - it's not guaranteed to be available on all installs.

#12 Updated by Daniel Sánchez Pillot Gutiérrez over 6 years ago

Are there any other alternatives for displaying a webview or webpage? I don't think changing would be much of a problem, I just went with what seemed the most obvious approach, if QtWebEngine shouldn't be used, does anybody know about an option?

#13 Updated by Nyall Dawson over 6 years ago

QtWebKit is the recommended approach here - it's what core qgis uses.

#14 Updated by simone massaro over 6 years ago

QtWebKit is not available on every Qt installation, from Qt 5.6 (the oldest supported LTS version) has been deprecated, so in many installations WebKit is not avalaible . Would it be possible to check at build time whether the Qt version has QwebKit o QWebEngine, and in the latter case do QWebEngine::initialaze()?

#15 Updated by Giovanni Manghi over 5 years ago

  • Status changed from Reopened to Feedback

Please check if this issue is still valid on QGIS 3.4.5 or 3.6.

#16 Updated by Klavs Christensen over 5 years ago

Giovanni Manghi wrote:

Please check if this issue is still valid on QGIS 3.4.5 or 3.6.

Am I supposed to be able to use QtWebEngineWidgets? I'm using 3.6.0-Noosa and get QtWebEngineWidgets must be imported before a QCoreApplication instance is created on

from PyQt5.QtWebEngineWidgets import QWebEngineView

WebKit works.

Also available in: Atom PDF