Opened 2 years ago

Last modified 19 months ago

#735 new defect

PyQt5 can't find DLL

Reported by: Andreas Müller Owned by: osgeo4w-dev@…
Priority: major Component: Package
Version: Keywords:
Cc:

Description (last modified by jef)

I'm running qgis-ltr 3.22. When I try to import from PyQt5, I get the error below:

    from PyQt5.QtCore import QSettings
    Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
    ImportError: DLL load failed while importing QtCore: Das angegebene Modul wurde nicht gefunden.

May be I'm missing something, so I can't find QtCore.dll in my install for example.

Change History (11)

comment:1 by jef, 2 years ago

Not reproducable here. The QtCore module file is called QtCore.pyd and the corresponding Qt DLL Qt5Core.dll

Last edited 2 years ago by jef (previous) (diff)

comment:2 by Andreas Müller, 2 years ago

If I use the Python Console inside QGIS, it works, calling python-qgis-ltr.bat on the OSGeo4w-Shell I get the error. PATH and variables all look fine.

comment:3 by jef, 2 years ago

Still can't reproduce. Running python-qgis-ltr.bat from OSGeo4W shell in a fresh install of qgis-ltr:

run o-help for a list of available commands
C:\OSGeo4W>python-qgis-ltr
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May  3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from PyQt5.QtCore import QSettings
>>> quit()

C:\OSGeo4W>type foo.py
from PyQt5.QtCore import QSettings

C:\OSGeo4W>python-qgis-ltr foo.py

C:\OSGeo4W>

comment:4 by jef, 2 years ago

Description: modified (diff)

comment:5 by Andreas Müller, 2 years ago

Thank you much, Jef, I did a fresh install and now both QGIS and python-qgis-ltr do work. Whatever I messed up! But what I actually wanted was to get PyScripter (4.1.1) working OSGeo4W python with your new installer and it's still complaining about the missing DLL. However, I think this is off topic?

Version 0, edited 2 years ago by Andreas Müller (next)

comment:6 by Andreas Müller, 2 years ago

The last try by myself was adding IDLE, which also works. I think there are two possible sources of error left: Something changed in PyScripter (filed PyScripter #1169) or in the Python install in OSGeo4W. (Third, I will eventually find a typo)

Last edited 2 years ago by jef (previous) (diff)

comment:7 by Andreas Müller, 2 years ago

I installed VSCode and used the approach provided here: https://github.com/MarByteBeep/pyqgis-standalone and described here: https://gis.stackexchange.com/questions/421362/looking-for-manual-on-how-to-properly-setup-standalone-pyqgis-without-gui

Running the example provided there (pyqgis-standalone script) is fine, using a script to import QSettings (as an example: from PyQt5.QtCore import QSettings) it fails with the same error.

[Added:] If I do from qgis.PyQt.QtCore import QSettings it works in "external" IDE's (PyScripter, VSCode)

Last edited 2 years ago by Andreas Müller (previous) (diff)

comment:8 by Andreas Müller, 2 years ago

New odd information, today I found this post https://gis.stackexchange.com/questions/431096/geopandas-error-oserror-could-not-find-or-load-spatialindex-c-64-dll-in-pycha reporting that geopandas didn't load in PyCharm. His solution was to import the processing module before importing geopandas. And what should I say: This works also when importing from QtCore! It seems that external IDE's share this "bug" and I guess they all use the python interpreter from apps\Python39 and not from bin\ like my PyScripter does.

comment:9 by Andreas Müller, 19 months ago

I found this in https://bugs.python.org/issue43173:

In 3.8+, the search path for the dependent DLLs of a normally imported extension module includes the following directories:

  • the loaded extension module's directory
  • the application directory (e.g. that of python.exe)
  • the user DLL search directories that get added by SetDllDirectory() and AddDllDirectory(), such as with os.add_dll_directory()
  • %SystemRoot%\System32

So the windows %PATH% has no longer an effect on finding DLL's. In case of using an external IDE which runs apps\Python39\python.exe you'll have to find a way to preload a python statement/script. For PyScripter I use the python_init.py with this statement import qgis.sys which seems to do the trick. However, not elegant.

in reply to:  9 comment:10 by jef, 19 months ago

Replying to Andreas Müller:

I found this in https://bugs.python.org/issue43173:

In 3.8+, the search path for the dependent DLLs of a normally imported extension module includes the following directories:

No news. import qgis does os.add_dll_directory() on Windows.

comment:11 by Andreas Müller, 19 months ago

Nice! Thank you for pointing me to that, jef.

Note: See TracTickets for help on using tickets.