pythonpython-3.xpyqt5qmlqapplication

'Module {Module_Name} not installed' when running python script using PyQt5


I am trying to build a UI using PyQt5 and python. I built a base using main.qml and some styled qml files in the same directory and it imported fine, but I decided to break everything up into folders so it is more organized but now none of my imports are working no matter what I do.

The new file structure is:

Interface/
│   ├── main.py                  
│   ├── resources.qrc            
│   ├── resources_rc.py          
│   │
│   ├── src/                     
│   │   ├── main.qml            
│   │   │
│   │   ├── Components/          # Directory for custom QML components
│   │   │   ├── Sections/        # Directory for "Sections" module
│   │   │   │   ├── Status.qml
                ├── Title.qml
│   │   │   │   └── qmldir       # qmldir file for "Sections" module
│   │   │   │
│   │   │   └── StyledElements/  # Directory for "StyledElements" module
│   │   │       ├── StyledButton.qml
│   │   │       └── qmldir

StyledElements & Sections contain a qmldir file that looks like:

module Sections
Status 1.0 Status.qml
Title 1.0 Title.qml

My main.py calls it like this:

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

def handle_warnings(warnings):
    for warning in warnings:
        print(f"QML Warning: {warning.toString()}")

app = QApplication([])

engine = QQmlApplicationEngine()
engine.warnings.connect(handle_warnings)

# Load QML file
qml_file_path = os.path.abspath('src/main.qml')
print(f"Loading QML file: {qml_file_path}")
engine.load(qml_file_path)

if not engine.rootObjects():
    print("Failed to load QML file.")
    sys.exit(-1)
else:
    print("QML file loaded successfully.")

app.exec_()

sys.exit(app.exec_())

And my main.qml looks like:

import QtQuick 2.15
import QtQuick.Controls 2.15
import StyledElements 1.0
import Sections 1.0

ApplicationWindow {
    title: 'Interceptor'
    visible: true
    width: 700
    height: 600

    Rectangle {
        id: rectangle
        width: 700
        height: 725
        color: 'white'

        Section_Title{}
        StyledButton{}
    }
}

Always gets me these errors:

QML Warning: module "Sections" is not installed
QML Warning: module "StyledElements" is not installed
QML Warning: module "Sections" is not installed
QML Warning: module "StyledElements" is not installed

Things I've tried:

I have tried putting quote around my import in my main.qml like:

import "StyledElements" 1.0
import "Sections" 1.0

Ive tried setting up import paths like so:

# Set the path to the QML files
engine.addImportPath(os.path.abspath("src/Components/Sections"))
engine.addImportPath(os.path.abspath("src/Components/StyledElements"))


# Load QML file
qml_file_path = os.path.abspath('src/main.qml')
engine.load(qml_file_path)

Also tried:

engine.addImportPath(os.path.abspath("qrc:/"))
or
engine.addImportPath(os.path.abspath("qrc:/src/main.qml"))

From what I can tell the issue is not to do with loading the main.qml because when i change the path it gives me an error about it not being found and the error seems to be coming from the import in the main.qml.

I tried using a resource file that looks like:

<RCC>
    <qresource prefix="/">
        <file>src/main.qml</file>
        <file>src/Components/Sections/NvStatus.qml</file>
        <file>src/Components/Sections/qmldir</file>
        <file>src/Components/StyledElements/StyledButton.qml</file>
        <file>src/Components/StyledElements/qmldir</file>
    </qresource>
</RCC>

I compiled resource file into python with pyrcc5 resources.qrc -o resources_rc.py

& imported the new py file using in main.py

import resources_rc

At this point ive spent hours with Chat GPT and google trying to find an answer. Ive tried multiple things that 'work' in the sense that they load the qml in the end but cant get the custom module to load. I am not sure how to proceed. Everything I try has lead me to the same issue of the main.qml unable to load the module with the errors showed above


Solution

  • After banging my head against the wall for a few more hours I figured it out:

    The big issue was with how I was importing my modules into the main.qml. It wants you to add the folders/subfolders in the import like so :

    Previously:

    import StyledElements 1.0
    import Sections 1.0
    

    Fixed:

    import src.Components.StyledElements 1.0
    import src.Components.Sections 1.0
    

    After that is was as easy as re-running my qrc conversion to a python script with :

    pyrcc5 resources.qrc -o resources_rc.py
    

    Found info here: https://doc.qt.io/qt-6/qtqml-modules-identifiedmodules.html