2
votes

I am getting these errors, when i try to launch the program:

plugin cannot be loaded for module, cannot install type into protected module

Platform:
Python 3.8
PyQt5 5.15.0
Visual Studio Community 2019
Windows 10 Pro 1909

main python file (the whole thing is pretty much the example from here: https://codeloop.org/pyqt5-creating-first-window/ )

import numpy
import os
import sys
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtWidgets import QApplication

def main():
    app =QApplication(sys.argv)
    engine = QQmlApplicationEngine()
    engine.load(os.path.join(os.path.dirname(__file__), "MainApp.qml"))
 
 
    if not engine.rootObjects():
        return -1
 
    return app.exec_()

if __name__ == '__main__':
    main(); 

The corresponding QML File "MainApp.qml":

import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick 2.15

Window {
 
    visible:true
    width:600
    height:400
    color:"yellow"
    title: "PyQt5 QML Window"
 
    Button {
        text: "Something"
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
    }
 
 
}

Without using anything from QtQuick.Controls, it works, but as soon as i add the button, it generates these errors:

QQmlApplicationEngine failed to load component
file:///C:/Users/elano/Source/Repos/Projekt-1-SS2020/Projekt-1-SS2020/MainApp.qml:15:5: Type Button unavailable
file:///C:/Users/elano/vpqt/lib/site-packages/PyQt5/Qt/qml/QtQuick/Controls.2/qmldir: plugin cannot be loaded for module "QtQuick.Controls": Cannot install type 'VerticalHeaderView' into protected module 'QtQuick.Controls' version '2'
<Unknown File>: Cannot install type 'HorizontalHeaderView' into protected module 'QtQuick.Controls' version '2'
<Unknown File>: Cannot install element 'SplitHandle' into protected module 'QtQuick.Controls' version '2'
...

The file it tries to find (qmldir) does exist, and contains this:

module QtQuick.Controls
plugin qtquickcontrols2plugin
classname QtQuickControls2Plugin
depends QtQuick.Templates 2.5
designersupported

Does anyone know what is going on here? Is more information needed?

1

1 Answers

2
votes

Okay, now i feel stupid. I already kinda had my answer in my code, but since it didn't get Pyside2 to work, i commented it out...
The problem was, that an environment variable was not set properly. Adding this after my import statements in my main file fixed it:

dirname = os.path.dirname(PyQt5.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QML2_IMPORT_PATH'] = os.path.join(dirname, 'qml')

No tutorial i found has ever mentioned this. Great.