PyQtWebEngine (Qt5)
PyQtWebEngine (Qt5) provides the Python bindings for Qt WebEngine, allowing the embedding of web content (based on Chromium) directly into PyQt5 applications. This package specifically targets applications built with PyQt5. It is maintained by Riverbank Computing and typically releases new versions in sync with PyQt5 and Qt5 patch releases.
Common errors
-
ModuleNotFoundError: No module named 'PyQt5.QtWebEngineWidgets'
cause The `pyqtwebengine-qt5` package is not installed, or the PyQt5 installation is corrupted/incomplete.fixRun `pip install pyqtwebengine-qt5` to ensure the package is installed correctly. -
AttributeError: 'PyQt5.QtWebEngineWidgets.QWebEngineView' object has no attribute 'show'
cause The `QApplication` instance has not been created or the event loop has not started, preventing GUI elements from being properly initialized or displayed.fixEnsure `app = QApplication(sys.argv)` is called before creating any widgets, and `app.exec_()` is called to start the event loop. -
ImportError: DLL load failed while importing QtWebEngineWidgets: The specified module could not be found.
cause This usually indicates an issue with the underlying Qt5 installation (often with PyQt5 itself) or conflicting Qt environments/DLLs on Windows, preventing the QtWebEngine part from loading.fixTry reinstalling both `pyqt5` and `pyqtwebengine-qt5` (`pip uninstall pyqtwebengine-qt5 pyqt5 && pip install pyqt5 pyqtwebengine-qt5`). Check for PATH conflicts with other Qt installations. In some cases, updating graphics drivers or ensuring system dependencies are met might help.
Warnings
- gotcha Installing `pyqtwebengine-qt5` does not automatically install `PyQt5`. You must install `PyQt5` separately if it's not already part of your environment.
- gotcha All PyQt applications, including those using PyQtWebEngine, require a `QApplication` instance to be created and an event loop to be run (`app.exec_()`) for the GUI to display and interact correctly.
- deprecated The `QWebEngineView.setUrl()` method is technically deprecated in newer Qt versions (and by extension, PyQt) in favor of `QWebEngineView.load(QUrl)`. While it still works in PyQt5, it's good practice to use `load()`.
Install
-
pip install pyqtwebengine-qt5 -
pip install pyqt5 pyqtwebengine-qt5
Imports
- QApplication
from PyQt5.QtWidgets import QApplication
- QWebEngineView
from pyqtwebengine import QWebEngineView
from PyQt5.QtWebEngineWidgets import QWebEngineView
- QUrl
from PyQt5.QtCore import QUrl
Quickstart
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl
def main():
app = QApplication(sys.argv)
browser = QWebEngineView()
browser.setUrl(QUrl('https://www.google.com')) # For PyQtWebEngine 5.15.x, setUrl is still commonly used.
browser.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()