在自定义Python3应用程序中实施qgis时,如何解决“在“”中找不到Qt平台插件“ Windows”?

时间:2019-01-10 16:13:50

标签: python-3.x qt qgis

我正在自定义应用程序中实现qgis 3.4.3,但是当我实例化QgsApplication()类时,收到错误消息“在“”中找不到Qt平台插件“ Windows”。

我尝试使用qgis 3.x +的各种安装,包括独立安装程序和OSGeo4W Web安装程序。我目前坚持使用OSGeo4W Web安装程序安装。我正在使用与OSGeo4W随附的Python 3.7安装分开,并尝试集成qgis功能。

我已遵循以下QGIS帮助文档中“在自定义应用程序中使用PyQGIS”部分下的说明。 https://docs.qgis.org/2.18/en/docs/pyqgis_developer_cookbook/intro.html#run-python-code-when-qgis-starts

尝试运行脚本后,我意识到缺少dll插件。经过研究,我发现了qt使用的qwindows.dll。 qwindows.dll包含在OSGeo4W安装中的以下位置:C:\ OSGeo4W \ apps \ Qt5 \ plugins \ platforms

我在本地命令提示符下更改了QT_PLUGIN_PATH变量,以包括上面的目录,但是普遍存在相同的错误。我还将QT_DEBUG_PLUGINS变量更改为1,该变量打印出Qt正在寻找插件的位置。有趣的是,它不在我在QT_PLUGIN_PATH变量中指定的路径中寻找插件。

Python代码:

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()

批处理文件代码以使用适当的变量启动Cmd提示符:

@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

我希望运行python脚本并且不会收到任何错误,因为我指向的是缺少qwindows.dll插件的存储目录。但是,我仍然收到丢失的Windows插件错误。

这是QT_DEBUG_PLUGINS设置为1的实际消息: QFactoryLoader :: QFactoryLoader()检查目录路径“ C:/ OSGeo4W / apps / qgis / qtplugins / platforms” ... QFactoryLoader :: QFactoryLoader()检查目录路径“ C:/ OSGeo4W / apps / Python37 / platforms” ... qt.qpa.plugin:在“”中找不到Qt平台插件“ windows” 此应用程序无法启动,因为无法初始化Qt平台插件。重新安装该应用程序可能会解决此问题。

1 个答案:

答案 0 :(得分:1)

在加载qgis.core之前,在python脚本的开头添加以下环境变量:

#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'

Qt使用QT_QPA_PLATFORM_PLUGIN_PATH变量来查找某些驱动程序,包括qwindows.dll。它不知道'〜qgis directory \ apps \ Qt5 \ plugins'目录的路径,因此在使用从qgis.core导入的QgsApplication模块之前,需要提供它。

为了使您的本地python版本找到qgis.core目录,您还需要在加载qgis.core之前将以下qgis目录添加到脚本中:

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