In QGIS 3.4, after creating a plugin with plugin builder, compiling it with pb_tools, I still can't load it to QGIS. I keep getting the following error message:

"Couldn't load plugin 'test_api' due to an error when calling its classFactory() method ImportError: PyQt4 classes cannot be imported in QGIS 3.x. Use PyQt5 or the version independent qgis.PyQt import instead."

I've already tried to search for the answer here but no success.

Here is how my .bat file looks like



set PATH=%PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
set PATH=%PATH%;C:\Program Files\7-Zip

@echo off 
call "%OSGEO4W_ROOT%\bin\o4w_env.bat" 
call "%OSGEO4W_ROOT%\bin\qt5_env.bat" 
call "%OSGEO4W_ROOT%\bin\py3_env.bat" 
@echo off 
path %OSGEO4W_ROOT%\apps\qgis-dev\bin;%OSGEO4W_ROOT%\apps\grass\grass-7.2.2\lib;%OSGEO4W_ROOT%\apps\grass\grass-7.2.2\bin;%PATH%

cd /d %~dp0

::start cmd

I guess the other setups are correct. What am I doing wrong?

  • And ? are sure your plugin is not calling PyQt4 ? – snaileater Apr 17 '19 at 18:41
  • It was in resources.py. I took a look at a functional 3.4 plugin and copied a similiar import, but still can't run. – Pedro Cury Apr 17 '19 at 18:49
  • Is it just the imports or class references and calls? I'm not sure how to find out – Pedro Cury Apr 17 '19 at 18:50
  • if your code is calling PyQt4 i fear QGIS (3 and later) won't agree with that ... – snaileater Apr 17 '19 at 19:00
  • I understand, but i'm not sure which lines I have to change besides "From PyQt4.imports", It's a simple plugin generated by the latest version of Plugin Buider. I didn't actually code yet. – Pedro Cury Apr 17 '19 at 19:06

Solved that calling:

pyrcc5 -o resources.py resources.qrc

I ran directly in my plugin folder. Apparently it modifies resources.py file to work with PyQt5. If the above commands generates a resources_rc file instead of altering the original, just copy the contents from resources_rc.py to resources.py or change the call from your main .py file. I'm not if that's best way but it worked for me.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.