Developing plugins and other Python scripts can be a daunting task for the uninitiated. Using an IDE (Integrated Development Environment) like PyCharm can help keep everything in one place and even integrates version control systems like Git. Unfortunately, unlike ArcPy you can’t just set the interpreter to the included Python 3.7 installation for QGIS and have PyCharm be aware of the QGIS modules.
While this isn’t strictly necessary, it does make the development process a touch nicer.
Identifying your paths
Not everyone installs QGIS to the default folder, nor does everyone install the same version of QGIS so it is imperative that you identify the following paths as they exist on your computer.
- QGIS Installation Folder
- PyCharm Installation Folder
You can do this by navigating in Windows Explorer and looking for those two applications. If you have installed the 64 bit versions of both software packages you will likely end up looking in the Program Files directory:
C:\Program Files\
However, if you installed the 32 bit versions of your software into a 64 bit windows environment you will likely have to look in the Program Files (x86) directory.
C:\Program Files (x86)\
PyCharm will be under a directory called Jetbrains. QGIS could be in any folder but if you have installed multiple versions of it make sure to select the proper installation.
Another common path for QGIS is under:
C:\OSGEO_4W\
Please note that this is in the root of the C: drive and not in Program Files
Regardless of where you have installed your respective software packages, copy the paths to a text file for future use.
Creating a batch (cmd) file
A batch file file is a command line script for Windows that runs commands as though you are typing them into the command prompt terminal. The process for creating them is quite simple, simply create a new text file anywhere on your computer by right clicking in the directory and selecting new -> Text Document (the desktop is a good place) and rename the file extension to “.cmd”
Right click your cmd file and select “Edit”. It will open up Notepad to edit your file.
Setting environmental variables with the batch file
Copy the following text into your batch file in Notepad replacing [PATHQGIS] with the path to the QGIS installation folder you identified previously and [PATHPYCHARM] with the PyCharm path:
@echo off
call “[PATHQGIS]”\bin\o4w_env.bat
call “[PATHQGIS]”\apps\grass\grass-7.4.0\etc\env.bat
@echo off
path %PATH%;[PATHQGIS]\apps\qgis\bin
path %PATH%;[PATHQGIS]\apps\grass\grass-7.4.0\lib
path %PATH%;[PATHQGIS]\apps\Qt5\bin
path %PATH%;[PATHQGIS]\apps\Python37\Scripts
set PYTHONPATH=%PYTHONPATH%;[PATHQGIS]\apps\qgis\python
set PYTHONHOME=[PATHQGIS]\apps\Python37
start “PyCharm aware of Quantum GIS” /B “[PATHPYCHARM]\bin\pycharm.exe”
Leave all quotation marks in place, they ensure spaces in the path are properly handled.
The file should look like this before you save it, with your proper paths in place.
The final start command in the file opens up PyCharm with connections to the qgis Python libraries.
Starting PyCharm for QGIS development
Keep this batch file in an easy to find location, or save it somewhere safe and create a shortcut to it. Double clicking it will open up PyCharm automatically. If you don’t use this batch file to open PyCharm it will not have the links to the qgis.core, qgis.gui, qgis.analysis or qgis.serv