在自定义 Python3 应用程序中实施 qgis 时如何修复 'Could not find the Qt platform plugin "Windows" in ""'?
How to fix 'Could not find the Qt platform plugin "Windows" in ""' when implementing qgis in custom Python3 application?
我在自定义应用程序中实施 qgis 3.4.3,但是当我实例化 QgsApplication() class 时,我在“”中收到错误 "Could not find the Qt platform plugin "Windows”。
我尝试使用各种 qgis 安装 3.x+,包括独立安装程序和 OSGeo4W 网络安装程序。我目前坚持使用 OSGeo4W 网络安装程序安装。我正在使用与 OSGeo4W 分开的 Python 3.7 安装,并尝试集成 qgis 功能。
我已按照以下 QGIS 帮助文档中的说明进行操作:"Using PyQGIS in Custom Applications"
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 平台插件。重新安装应用程序可能会解决此问题。
在加载 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'])
我在自定义应用程序中实施 qgis 3.4.3,但是当我实例化 QgsApplication() class 时,我在“”中收到错误 "Could not find the Qt platform plugin "Windows”。
我尝试使用各种 qgis 安装 3.x+,包括独立安装程序和 OSGeo4W 网络安装程序。我目前坚持使用 OSGeo4W 网络安装程序安装。我正在使用与 OSGeo4W 分开的 Python 3.7 安装,并尝试集成 qgis 功能。
我已按照以下 QGIS 帮助文档中的说明进行操作:"Using PyQGIS in Custom Applications" 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 平台插件。重新安装应用程序可能会解决此问题。
在加载 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'])