8

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 location
QgsApplication.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
@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.

1 Answer 1

9

Add the following environment variables at the beginning of your python script before loading qgis.core:

#modify environment variables to find qgis and qt plugins during qgis.core import
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = r'~qgis directory\apps\Qt5\plugins'
os.environ['PATH'] += r';~qgis directory\apps\qgis\bin;~qgis directory\apps\Qt5\bin'

The QT_QPA_PLATFORM_PLUGIN_PATH variable is used by Qt to look up certain drivers including the qwindows.dll. It doesn't know the path to the '~qgis directory\apps\Qt5\plugins' directory, so you need to provide it before using the QgsApplication module imported from qgis.core.

In order for your local version of python to find the qgis.core directory, you will also need to add the following qgis directories to your script before loading qgis.core:

sys.path.extend([r'~qgis directory\apps\qgis\python',r'~qgis directory\apps\Python37\Lib\site-packages'])

Edit: Note: fill in the details of ~qgis directory for your QGIS installation, e.g. C:\Program Files\QGIS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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