setting pycharm as a pyqgis ide

Solutions on MaxInterview for setting pycharm as a pyqgis ide by the best coders in the world

showing results for - "setting pycharm as a pyqgis ide"
Salomé
20 Feb 2017
1tand-alone PyQGIS scripts with OSGeo4W
2PyQGIS scripts are great to automate spatial processing workflows. It’s easy to run these scripts inside QGIS but it can be even more convenient to run PyQGIS scripts without even having to launch QGIS. To create a so-called “stand-alone” PyQGIS script, there are a few things that need to be taken care of. The following steps show how to set up PyCharm for stand-alone PyQGIS development on Windows10 with OSGeo4W.
3
4An essential first step is to ensure that all environment variables are set correctly. The most reliable approach is to go to C:\OSGeo4W64\bin (or wherever OSGeo4W is installed on your machine), make a copy of qgis-dev-g7.bat (or any other QGIS version that you have installed) and rename it to pycharm.bat:
5
6
7
8Instead of launching QGIS, we want that pycharm.bat launches PyCharm. Therefore, we edit the final line in the .bat file to start pycharm64.exe:
9
10
11
12In PyCharm itself, the main task to finish our setup is configuring the project interpreter:
13
14
15
16First, we add a new “system interpreter” for Python 3.7 using the corresponding OSGeo4W Python installation.
17
18
19
20To finish the interpreter config, we need to add two additional paths pointing to QGIS\python and QGIS\python\plugins:
21
22
23
24That’s it! Now we can start developing our stand-alone PyQGIS script.
25
26The following example shows the necessary steps, particularly:
27
28Initializing QGIS
29Initializing Processing
30Running a Processing algorithm
311
322
333
344
355
366
377
388
399
4010
4111
4212
4313
4414
4515
4616
4717
4818
4919
5020
5121
5222
5323
5424
5525
5626
5727
5828
5929
6030
6131
6232
63import sys
64 
65from qgis.core import QgsApplication, QgsProcessingFeedback
66from qgis.analysis import QgsNativeAlgorithms
67 
68QgsApplication.setPrefixPath(r'C:\OSGeo4W64\apps\qgis-dev', True)
69qgs = QgsApplication([], False)
70qgs.initQgis()
71 
72# Add the path to processing so we can import it next
73sys.path.append(r'C:\OSGeo4W64\apps\qgis-dev\python\plugins')
74# Imports usually should be at the top of a script but this unconventional 
75# order is necessary here because QGIS has to be initialized first
76import processing
77from processing.core.Processing import Processing
78 
79Processing.initialize()
80QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
81feedback = QgsProcessingFeedback()
82 
83rivers = r'D:\Documents\Geodata\NaturalEarthData\Natural_Earth_quick_start\10m_physical\ne_10m_rivers_lake_centerlines.shp'
84output = r'D:\Documents\Geodata\temp\danube3.shp'
85expression = "name LIKE '%Danube%'"
86 
87danube = processing.run(
88    'native:extractbyexpression',
89    {'INPUT': rivers, 'EXPRESSION': expression, 'OUTPUT': output},
90    feedback=feedback
91    )['OUTPUT']
92 
93print(danube)
94<div class="open_grepper_editor" title="Edit & Save To Grepper"></div>
95
similar questions
queries leading to this page
setting pycharm as a pyqgis ide