How to set environment for PyQGIS 3 in PyCharm

Prerequirement
- Install PyCharm
- Install OSGeo4W Network Installer
- choose QGIS desktop and QGIS full desktop
Setting the Environment
Create pygis.cmd
To setup the environment for PyQGIS in windows, you should get into the directory of ./OSGeo4W
and create a pyqgis.cmd
script. The following script is written by me according to the computer environment. Another to create a .cmd
script is to get into the folder ./OSGeo4W/bin/python.bat
and copy it to the main directory ./OSGeo4W
. You should customize the set PATH
statement to add any paths you want available when working from the command line. I added paths to my git
install.
@echo off
SET OSGEO4W_ROOT=D:\OSGeo4W64
call "%OSGEO4W_ROOT%"\bin\o4w_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%;D:\OSGeo4W64\apps\Qt5\bin
path %PATH%;D:\OSGeo4W64\apps\Python36\Scripts
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python36
set PATH=C:\Program Files\Git\bin;%PATH%
cmd.exe
Through this .cmd
, you can get into python3
and test if qgis can be imported. If you get errors when you import PyQt5.QtCore
, don’t worry, we will fix in the next step.
C:\Users\gsherman>python3
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import qgis.core
>>> import PyQt5.QtCore
Besides, you can install third packages through this command line script.
python3 -m pip install pandas
python3 -m pip install pb_tool
Create pycharm.cmd
By adding one line at the end of pyqgis.cmd
, we can start IDE with recognising the QGIS libraries:
start "PyCharm aware of Quantum GIS" /B %PYCHARMPATH%
Please remember replace %PYCHARMPATH%
as your own path where you installed PyCharm and save as new file pycharm.cmd
. Within your IDE settings, point it to use the Python interpreter included with OSGeo4W—typically at: %OSGEO4W_ROOT%\bin\python3.exe
. This will make it pick up all the QGIS goodies needed for development, completion, and debugging. Another important notice place here is that you should add QT_PLUGIN_PATH
in this file.
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
Here is the full script for pycharm.cmd
, please note that my OSGEO4W_ROOT is C:\OSGeo4W64
and grass version is grass78
and python version is Python37
.
@echo off
SET OSGEO4W_ROOT=D:\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%;D:\OSGeo4W64\apps\Qt5\bin
path %PATH%;D:\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"
Setting PyCharm IDE
Just double-click pycharm.cmd
to start PyCharm. In the settings of PyCharm project, set interpreter as %OSGEO4W_ROOT%\bin\python3.exe
. In my computer, the full path of interpreter is D:\OSGeo4W64\bin\python3.exe.