1

I'm constructing an application via Python as backend and QT QML as frontend. I use Win10, PyCharm 2022.3.1 (Community Edition), Pyside6 and QT Design Studio (not QT Creator). All libraries installed by pip using terminal or through PyCharm library manager.
Everything was fine exactly until I tried to import a separate Python file with code designed to send messages to the CAN bus using a third-party python library canlib by Kvaser. Other imported libraries such as Pandas or ElementTree work fine.
The issue is that the QQmlApplicationEngine failed to load component when I'm trying to import this file.

The error from terminal sounds like: (venv) PS C:\Users\Alexa\PycharmProjects\LEET\venv> python LEET.py QQmlApplicationEngine failed to load component file:///D:/LEET/QtDesignProjects/LEET/LEET.qml:1:1: Cannot load library C:\Users\Alexa\PycharmProjects\LEET\venv\lib\site-p ackages\PySide6\qml\QtQuick\qtquick2plugin.dll

My main file (LEET.py):

import sys
from PySide6.QtGui import QGuiApplication, QIcon
from PySide6.QtQml import QQmlApplicationEngine, QmlElement, qmlRegisterType

from Broadcast import BroadcastRequest # here I try to import file with this external library

qmlRegisterType(BroadcastRequest, 'sendSingle', 1, 0, 'BroadcastRequest')

app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()

app.setWindowIcon(QIcon("D:\\Midjourney_logos\\LEET_icon_small.png"))

\#Get Context
broadcastSingleMessage = BroadcastRequest()
engine.rootContext().setContextProperty('manualSendConnection', broadcastSingleMessage)

engine.load('D:\\LEET\\QtDesignProjects\\LEET\\LEET.qml')

if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())

My Broadcast.py starts like this:

from canlib import canlib # this is a root cause. If I comment out this canlib import, problem disappears                   

My QML file starts like this:

import QtQuick 2.0 # engine stops here, but I'm not sure that this is a reason
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.0
import QtQuick.Window 2.3

I discovered that if I try to import canlib from canlib, engine fails. If I erase this import, no error appears. Maybe there's any incompatibility between QML and canlib? On kvaser (lib manufacturer) site it's mentioned that module canlib.canlib located in canlib32.dll. 32bits is a reason? Update: I've found in driver installation that it selects 32/64 automatically depends of Win version so it's 64bits dll obviously. PyCharm shows no error also, all works great, just QML engine has this issue.

  • What is `DstPivot`? – ניר Feb 09 '23 at 09:40
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Feb 09 '23 at 10:07
  • DstPivot is another python file which do necessary things with some xml files uses pandas and ElementTree libraries. It's connected to one of an interface buttons and works without complaint. I deleted rows with DstPivot to make the code easier. – Alexander Poverennov Feb 09 '23 at 10:30

1 Answers1

1

It's a bug in the canlib. The cause is calling the WIN32 API SetDefaultDllDirectories() as you can see at this. I recommend to report the bug on this.

The remedy will be to import the canlib after the Qt loads all the libraries. Since you are using QML, one of the tricks will be to load a dummy QML that does not depends on the canlib before importing the canlib.

relent95
  • 3,703
  • 1
  • 14
  • 17
  • Thank you very much, I've sent a bug report. Could you tell me please how to import the canlib after QT library loading? Can I import broadcast.py and update properties after engine.load operation? Maybe I didnt' catch a point. – Alexander Poverennov Feb 10 '23 at 06:56
  • You mean changing the ```BroadcastRequest``` so it lazy loads the ```canlib```? That's another way to solve the problem. What I suggested was to create a dummy(unused) QML and load it before loading the original QML(hence loading the ```canlib```). – relent95 Feb 10 '23 at 07:13
  • Ok, I've replaced import and context update just right after engine.load and now it works. Magic. I'll keep to apply my efforts and will comment here if the problem is solved. – Alexander Poverennov Feb 10 '23 at 07:22
  • @AlexanderPoverennov Remember that while, theoretically, the order of imports should not alter the behavior of the program, that isn't always true. Some libraries are dependent on others and change their behavior depending on the current environment (including that of the interpreter process). For instance, some libraries that use Qt (notably, pyqtgraph or matplotlib, when using Qt backend) should *always* be imported *after* importing the Qt binding (PySide or PyQt), as the user could have different binding and versions installed. – musicamante Feb 10 '23 at 17:14