12

I'm referring to this post:

Why does QGIS 3.2 "native:extractvertices" algorithm not work properly in standalone script?

as part on creating startup file py3-env.bat for opening PyCharm with PyQGIS works:

@ECHO OFF

set OSGEO4W_ROOT=C:\OSGeo4W64

@echo off 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 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 PYCHARM="C:\Program Files\JetBrains\PyCharm Community Edition with Anaconda plugin 2020.1\bin\pycharm64.exe"

set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37 set PYTHONPATH=%OSGEO4W_ROOT%\apps\Python37\lib\site-packages;%PYTHONPATH%

set QT_QPA_PLATFORM_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\Qt5\plugins\platforms set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis

cd /d %~dp0 ::python3 scratch.py ::pause start "PyCharm aware of QGIS" /B %PYCHARM% %*

I have:

QGIS 3.12.1 installed in: C:\OSGeo4W64

PyCharm 2020.1 installed in: C:\Program Files\JetBrains\PyCharm Community Edition with Anaconda plugin 2020.1\

After loading PyCharm the interpreter looks like this:

enter image description here

Now I just would like to run a simple test script like:

#native:extractvertices
import sys

from qgis.core import ( QgsApplication, QgsProcessingFeedback, QgsVectorLayer ) from qgis.analysis import QgsNativeAlgorithms

QgsApplication.setPrefixPath(r'C:\OSGeo4W64\apps\qgis', True) qgs = QgsApplication([], False) qgs.initQgis()

sys.path.append(r'C:\OSGeo4W64\apps\qgis\python\plugins')

import processing from processing.core.Processing import Processing Processing.initialize() QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

layer = QgsVectorLayer(r"D:\test_pygis\lines.shp", 'my layer', 'ogr') output = r"D:\test_pygis\verticles.shp" params = { 'INPUT': layer, 'OUTPUT': output, } feedback = QgsProcessingFeedback() res = processing.run("native:extractvertices", params, feedback=feedback) print(res)

First the processing module is not found:

enter image description here

If I try to run the code the second well know error appears in the Python console:

qt.qpa.plugin: Could not find the Qt platform plugin "windows" in "C:\OSGEO4~1\apps\Qt5\plugins\platforms"
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
bugmenot123
  • 11,011
  • 3
  • 34
  • 69
cincin21
  • 149
  • 1
  • 8

3 Answers3

11

The Most simply solution without the need to set an own bat-script I have found in the comments of this video: https://www.youtube.com/watch?v=Qm0PCw9v5lI

Just add a new Python Interpreter within PyCharm and select the file python-qgis-ltr.bat which you can find within the bin folder of your QGIS installation directory. All the variables in relation to this QGIS installation are set there already.

enter image description here

danceb
  • 456
  • 1
  • 4
  • 11
  • 1
    Thanks for this. In my installation of qgis 3.22.1 the script is called python-qgis.bat. – Richard A Oct 30 '22 at 13:37
  • Does this allow PyCharm to recognize QGIS' classes and provide introspection and auto-complete? – bugmenot123 Nov 25 '22 at 16:01
  • Yes, but it is not including the processing module, which makes sense as that is a "plugin" to the rest, requires an initialized QgsApplication anyways and does not have much to auto-complete and introspect anyways. – bugmenot123 Dec 06 '22 at 14:38
  • If I configure it like this, it seems as if it works properly. However, it doesn't update the view of the open QGIS instance, e.g. no layer are added to the TOC. Is this correct, or did I miss here something? – i.i.k. Apr 25 '23 at 10:51
  • @i.i.k.: This is not a way to remotely control an already running, independent QGIS. You are able to breakpoint and manipulate a QGIS instance spawned from PyCharm though. – bugmenot123 May 10 '23 at 07:48
  • 1
    @bugmenot123 Yeah, in the meantime, I learned, that you can refer to the running project only if you run the code inside of QGIS, otherwise you will get an error in Pycharm. I have given my actual setup below. – i.i.k. May 10 '23 at 10:18
10

I would like to share the solution I found to integrate PyCharm and PyQGIS in an effective way to run standalone scripts:

Setup:

  • Windows 10
  • QGIS 3.10.4 installed in c:\Program Files\QGIS 3.10
  • PyCharm 2020.1.1 installed in c:\Program Files\JetBrains\PyCharm Community Edition 2020.1.1\bin\pycharm64.exe

I can start PyCharm from a batch file (e.g., pycharm_qgis.bat) that looks like the following (note that the QT_PLUGIN_PATH is slightly different than in your case):

@echo off 
set OSGEO4W_ROOT=c:\PROGRA~1\QGIS3~1.10
set path=%OSGEO4W_ROOT%\bin;%WINDIR%\system32;%WINDIR%;%WINDIR%\system32\WBem

call o4w_env.bat call qt5_env.bat call py3_env.bat

@echo off path %OSGEO4W_ROOT%\apps\qgis-ltr\bin;%PATH% set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:=/%/apps/qgis-ltr set GDAL_FILENAME_IS_UTF8=YES set VSI_CACHE=TRUE set VSI_CACHE_SIZE=1000000 set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis-ltr\qtplugins;%QT_PLUGIN_PATH% set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis-ltr\python;%PYTHONPATH%

set PYCHARM="c:\Program Files\JetBrains\PyCharm Community Edition 2020.1.1\bin\pycharm64.exe" @echo on start "PyCharm with QGIS knowledge!" /B %PYCHARM% %*

From the code above, the o4w_env.bat, qt5_env.bat, py3_env.bat are the same batch files available in the QGIS binary folder (i.e., in the case of this example %OSGEO4W_ROOT%\apps\qgis-ltr\bin) without any modification. By executing pycharm_qgis.bat PyCharm starts and the Python interpreter can be set to %OSGEO4W_ROOT%\apps\Python37\python.exe in order to work with PyQGIS library.

Then, for PyCharm to recognize and use the processing module, one needs to manually add to the interpreter path the folder %OSGEO4W_ROOT%\apps\qgis-ltr\python\plugins, as shown in the next picture:

Manually add the processing module folder to Python interpreter path

After that, PyCharm can interact with the processing module:

PyCharm recognies the processing module

Finally, the following code excerpt replicates your example and uses the native:extractvertices algorithm:

import os
import sys

from qgis.analysis import QgsNativeAlgorithms from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingException, QgsProcessingFeedback

if name == "main": QgsApplication.setPrefixPath(r"c:\Program Files\QGIS 3.10\apps\qgis-ltr", True) qgs = QgsApplication([], False) qgs.initQgis() sys.path.append(os.path.join(QgsApplication.prefixPath(), "python", "plugins"))

import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

s_lyr_gpk = os.path.join("Shp", "BOUNDARIES3D.shp")
s_lyr_name = "Boundaries"
lyr = QgsVectorLayer(s_lyr_gpk, s_lyr_name, 'ogr')
if not lyr.isValid():
    print("Layer {} is not valid".format(s_lyr_name))

s_alg = 'native:extractvertices'
out = os.path.join("d:\Data\Output", "extracted_vertices_script.gpkg")

di_param = {
     "INPUT": lyr,
     "OUTPUT": out
}
feedback = QgsProcessingFeedback()
try:
    di_out = processing.run(s_alg, di_param, feedback=feedback)
except QgsProcessingException as e:
    print(e)
    qgs.exitQgis()
    sys.exit(1)

print("Vertices extracted to file {}.".format(di_out["OUTPUT"]))
qgs.exitQgis()

The above script can be executed directly from PyCharm to get the output geopackage written. No error related to missing QT platform plugin is raised (see image below):

Successful script execution from PyCharm

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
fastest
  • 1,289
  • 7
  • 14
  • Thank you, give me some time to test! – cincin21 May 28 '20 at 09:11
  • Hi Cincin, let me know if any additional code snippet or explanation is needed. Cheers – fastest May 28 '20 at 14:06
  • I believe it would be beneficial to paste whole code with import that others could copy-paste the solution – cincin21 May 28 '20 at 14:08
  • Hi Cincin, I did specify the content of the other bat files included in the file used to start PyCharm. Moreover, I added the import statement to the Python code example and enclosed it in a main function so that it can be executed as a standalone script. Cheers. – fastest May 28 '20 at 14:38
  • Great stuff! I would only add one path to Interpreter paths: C:\Program Files\QGIS 3.10\apps\qgis-ltr\python , I've seen it in other solutions and after adding this one, everything started to work. Thank you very much! – cincin21 May 28 '20 at 20:27
  • @fastest, can you tell me please how does the .bat-file work? – Taras May 29 '20 at 05:10
  • @cincin21: thank you, I am glad I could help! The path you mention is indeed needed in the Python interpreter and it is included in my code to start PyChrarm (i.e., the 4th last line set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis-ltr\python;%PYTHONPATH%.

    @Taras: the .bat file to start PyCharm can be executed by double clicking on it. Afterwards, PyCharm should start and be already informed about the paths to the PyQGIS library. Please, let me know if more information are required.

    Cheers

    – fastest May 29 '20 at 13:21
4

System Windows QGIS 3.28 Firenze

For me I seem to be able to use the qgis functionality as well as the processing plugin in PyCharm when I do the following:

  1. Set the project interpreter in Pycharm to use the batchfile under C:/Program Files/QGIS 3.28.3/bin/python-qgis.bat.

enter image description here

  1. Add manually the path C:/Program Files/QGIS 3.28.3/apps/qgis/python/plugins to the interpreter path

enter image description here

  1. In your python script, to be able to use the algorithms from the processing plugin, import all functionality from the qgis.core module as well as the processing module and initialize it.
from qgis.core import *

import processing from processing.core.Processing import Processing Processing.initialize()

I just tried this out without any deeper knowledge about what I'm actually doing here, and didn't had the time since yesterday to do more research on it yet, but it works for me so far.

i.i.k.
  • 1,427
  • 5
  • 11
  • It works beautify! Thank you for all the details. However, I have a tiny question. If I would like to install additionally a certain module, let's say geopy, how should it be executed? Do you know, is it possible to have the PyQGIS functionality within a venv? – Taras Jan 09 '24 at 11:38
  • 1
    @Taras I'm very sure, this must be possible. Mh, I'm using conda virtual environments, and I'm using pyqgis generally in Jupyter Notebooks and rarely in PyCharm anymore. In conda you can install pyqgis in your environment with this command: conda install qgis=3.28.5 --channel conda-forge. Maybe you can adjust it to venv? The relevant venv should then be able to be set as the interpreter in Pycharm. In this venv you could also install geopy then. – i.i.k. Jan 09 '24 at 11:57
  • Nice idea! Thank you :) – Taras Jan 09 '24 at 12:56
  • Also can be useful: https://plugins.qgis.org/planet/tag/conda/ and https://stackoverflow.com/questions/35622661/import-qgis-modules-into-python-anaconda – Taras Jan 09 '24 at 13:51