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):
ForFrom 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, I needone needs to manually add to the interpreter path the folder %OSGEO4W_ROOT%\apps\qgis-ltr\python\plugins, as shown in the next picture:
Finally, I prepared a smallthe following code excerpt that replicates your example and uses the native:extractvertices
algorithm:
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()
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()
I can run thisThe above script can be executed directly from PyCharm andto get the output geopackage file correctly written. In my case, noNo error related to missing QT platform plugin is raised (see image below):