[Qgis-user] How to fix 'Could not find the Qt platform plugin “Windows” in “”' when implementing qgis in custom Python3 application?

DT dmofot at gmail.com
Thu Jan 10 13:58:51 PST 2019


Matt, I don’t know if this was specific to OSX or of it might be relevant
to your issue on Windows, but I had to set another Qt path.  See this gist:
https://gist.github.com/dmofot/94345126db10660053ccfb0d76a1e32a

DT

On Thu, Jan 10, 2019 at 4:44 PM Matt Brauer <jbrauer90 at gmail.com> wrote:

> I am implementing qgis 3.4.3 in a custom application, but when I
> instantiate the QgsApplication() class, I receive the error "Could not find
> the Qt platform plugin "Windows" in "".
>
> I've attempted using various installs of qgis 3.x+ including the
> standalone installer and OSGeo4W web installer. I currently am sticking
> with the OSGeo4W web installer installation. I am using a Python 3.7
> installation separate from what comes with OSGeo4W and attempting to
> integrate qgis functionality.
>
> I have followed the instructions in the following QGIS help doc under the
> section: "Using PyQGIS in Custom Applications"
> https://docs.qgis.org/2.18/en/docs/pyqgis_developer_cookbook/intro.html#run-python-code-when-qgis-starts
>
> After attempting to run the script I realized a dll plugin was missing.
> After some research, I found its the qwindows.dll that qt uses. The
> qwindows.dll is included in the OSGeo4W installation under:
> C:\OSGeo4W\apps\Qt5\plugins\platforms
>
> I changed the QT_PLUGIN_PATH variable on the local command prompt to
> include the directory above, but the same error prevailed. I also changed
> the QT_DEBUG_PLUGINS variable to 1 which printed out the locations Qt is
> looking for plugins. Interestingly, it wasn't looking for plugins in the
> path I specified in the QT_PLUGIN_PATH variable.
>
> Python Code:
>
> import sys#add qgis python libraries to instance python path in case not added #at the environment variable %PYTHONPATH% level
> sys.path.extend(['C:\\OSGeo4W\\apps\\qgis\\python', 'C:\\OSGeo4W\\apps\\Python37\\Lib\\site-packages'])
> from qgis.core import *
> # supply path to qgis install locationQgsApplication.setPrefixPath('C:\\OSGeo4W\\apps\\qgis', True)
> # create a reference to the QgsApplication# setting the second argument to True enables the GUI, which we need to do# since this is a custom application
> qgs = QgsApplication([], True)
> # load providers
> qgs.initQgis()
> # Write your code here to load some layers, use processing algorithms, etc.
> # When your script is complete, call exitQgis() to remove the provider and# layer registries from memory
> qgs.exitQgis()
>
> Batch File Code to Start Cmd Prompt with Proper Variables:
>
> @echo off
> SET OSGEO4W_ROOT=C:\OSGeo4W::Include environment variables set by qgis on startup
> call %OSGEO4W_ROOT%\bin\o4w_env.bat
> call %OSGEO4W_ROOT%\bin\qt5_env.bat
> call %OSGEO4W_ROOT%\bin\py3_env.bat at echo off
> path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
> path %PATH%;C:\OSGeo4W\apps\Qt5\bin
> path %PATH%;C:\OSGeo4W\apps\Python37\Scripts
> path %PATH%;C:\OSGeo4W\apps\Python37
>
> set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
> set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
> set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\Qt5\plugins;%OSGEO4W_ROOT%\apps\qgis\qtplugins
> set QT_DEBUG_PLUGINS=1
>
> cmd.exe
>
> I expect to run the python script and receive no errors since I'm pointing
> to the directory where the qwindows.dll missing plugin is stored. However,
> I am still receiving the missing windows plugin error.
>
> Here is the actual message with QT_DEBUG_PLUGINS set to 1:
> QFactoryLoader::QFactoryLoader() checking directory path
> "C:/OSGeo4W/apps/qgis/qtplugins/platforms" ...
> QFactoryLoader::QFactoryLoader() checking directory path
> "C:/OSGeo4W/apps/Python37/platforms" ... qt.qpa.plugin: Could not find the
> Qt platform plugin "windows" in "" This application failed to start because
> no Qt platform plugin could be initialized. Reinstalling the application
> may fix this problem.
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20190110/2b30de1b/attachment-0001.html>


More information about the Qgis-user mailing list