1

I followed these getting started instructions to create a standalone script in pycharm.

The only thing I did differently was to download the standalone installer, not the network installer.

I'm trying to run this code from pyqgis_developer_cookbook.

from qgis.core import *
QgsApplication.setPrefixPath(r"C:\Program Files\QGIS 3.4\apps\qgis", True)
qgs = QgsApplication([], False)
qgs.initQgis()
qgs.exitQgis()

But when it runs qgs = QgsApplication([], False), an error pops up:

This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

enter image description here

  • Should the "r" stay here? the sample code in the cookbook doesn't have that "r" before the path to qgis installation. (r"C:\Program Files\QGIS 3.4\apps\qgis", True) – Vale Nov 15 '18 at 13:35
1

You are getting that error because you are missing a line in your batch file which sets the Qt Plugin Path. The batch/ command file suggested in the link you posted does not contain that line. The line in the batch file for my qgis installation looks like this:

set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins

However, your paths would be different as you say you used the standalone installer.

What I would suggest you do is to find the python-qgis.bat file located in the bin folder of your qgis installation.

In your case, since you used the standalone installer, the batch file should be found in C:\Program Files\QGIS 3.4\bin\python-qgis.bat

Open this batch file in a text editor and copy the line which begins with: set QT_PLUGIN_PATH=

Edit your current batch file which you have created based on the instructions in Gary Sherman's blog by pasting in the line which sets the Qt Plugin Path and save. However, without knowing what your batch file looks like, you may still have other problems. All the paths for your installation will be different to those in Gary's blog because you used the standalone installer. In the long run, you may find it easier to re-install QGIS with the OSGEO4W installer.

I have an OSGEO4W install and, using this batch script to start PyCharm:

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W64
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%"\bin\qt5_env.bat
call "%OSGEO4W_ROOT%"\bin\py3_env.bat
call "%OSGEO4W_ROOT%"\apps\grass\grass-7.4.0\etc\env.bat
@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass-7.4.0\lib
path %PATH%;C:\OSGeo4W64\apps\Qt5\bin
path %PATH%;C:\OSGeo4W64\apps\Python36\Scripts

set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python36
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins

start "PyCharm aware of QGIS" /B "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.3\bin\pycharm.exe"

I can run:

from qgis.core import *
QgsApplication.setPrefixPath("C:/OSGeo4W/apps/qgis/", True)
qgs = QgsApplication([], False)
qgs.initQgis()
qgs.exitQgis()

with no errors.

  • You are really a great help. Problem is solved. – swdmike Nov 16 '18 at 7:10
1

Below is my bat file inspired by Ben W's answer.

It is based on python-qgis.bat in bin folder, different from Gary Sherman's blog, and it works.

For anyone who has the similar problem.

@echo off
SET PYCHARM="C:\Program Files\JetBrains\PyCharm 2018.2.5\bin\pycharm64.exe"
SET OSGEO4W_ROOT=C:\Program Files\QGIS 3.4
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\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis
set GDAL_FILENAME_IS_UTF8=YES
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH%

start "PyCharm aware of Quantum GIS" /B %PYCHARM% %*

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.