PyCharm – Adding QGIS-Specific Python Interpreter to PyCharm on Windows

installationpycharmpyqgisqgis

I am having trouble adding a QGIS-specific Python interpreter to PyCharm. Ideally, I would like to be able to switch between both Python2 and Python3 in PyCharm. I thought this might be possible by setting the .exe files from C:\OSGeo4W64\apps\Python27 and C:\OSGeo4W64\apps\Python36 as Python interpreters. However, none of this works, the error always looks like here below, where I tried to add C:\OSGeo4W64\bin\python.exe
enter image description here


After reading through some blogs and questions (links below), here is what I tried:

1) I created this pycharm-pyqgis.bat file below, that I am now using to start PyCharm.

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W
SET QGISNAME=qgis
SET QGIS=%OSGEO4W_ROOT%apps%QGISNAME%
SET QGIS_PREFIX_PATH=%QGIS%
SET PYCHARM="C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.1\bin\pycharm.exe"

CALL qt5_env.bat
CALL py3_env.bat

CALL %OSGEO4W_ROOT%bin\o4w_env.bat

SET PATH=%PATH%;%QGIS%\bin
SET PATH=%PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
SET PYTHONPATH=%QGIS%\python
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27\Scripts
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27\Lib
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27\Lib\site-packages
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27\DLLs
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis-dev\python

start "PyCharm aware of QGIS" /B %PYCHARM% %*

2) I modified the Windows environment variables to the folders indicated above (C:\OSGeo4W64\bin..). However, I am a little uncertain what should really go into PATH, PYTHONPATH, PYTHONHOME and PYTHON.


Related Links


This is not a working answer, just an extended reply to the comment above from Andreas Müller.

I now created two batch files as Andreas Müller suggested in the comment above.

Python27

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W
SET QGISNAME=qgis
SET QGIS=%OSGEO4W_ROOT%apps%QGISNAME%
SET QGIS_PREFIX_PATH=%QGIS%
SET PYCHARM="C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.1\bin\pycharm.exe"

CALL %OSGEO4W_ROOT%bin\o4w_env.bat

SET PATH=%PATH%;%QGIS%\bin
SET PATH=%PATH%;%OSGEO4W_ROOT%\apps\qgis\bin

SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27\Scripts
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27\Lib
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27\Lib\site-packages
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27\DLLs

start "PyCharm aware of QGIS" /B %PYCHARM% %*

Python36

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W
SET QGISNAME=qgis
SET QGIS=%OSGEO4W_ROOT%apps%QGISNAME%
SET QGIS_PREFIX_PATH=%QGIS%
SET PYCHARM="C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.1\bin\pycharm.exe"

CALL qt5_env.bat
CALL py3_env.bat

CALL %OSGEO4W_ROOT%bin\o4w_env.bat

SET PATH=%PATH%;%QGIS%\bin
SET PATH=%PATH%;%OSGEO4W_ROOT%\apps\qgis\bin

SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python36
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python36\Scripts
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python36\lib
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python36\Lib\site-packages
SET PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python36\DLLs

start "PyCharm aware of QGIS" /B %PYCHARM% %*

I am not sure if I did it correctly, but it doesn't solve the problem:

enter image description here

Best Answer

At least i managed to start Pycharm with an OSGEO4W Environment. My PyCharm is 2017.3, 64bit-Edition. My OSGEO4W is 32bit and uses QGIS3/Python3. I think it will be easy to change it for QGIS2/Python2.7 or anything with 64bit. I do start PyCharm with the batch-file in PyCharms bin directory, called by a custom Script to set the needed environment variables, allowing to make QGIS-Plugins with Qt and QGIS-Standalone-Applications. When PyCharm opens I create a new Project and set the Interpreter to the QGIS python in %OSGEO4W_ROOT%\apps\Python36.

This is the batch file content:

@echo off
REM CHANGE THESE
SET OSGEO4W_ROOT=C:\OSGeo4W64
SET PYCHARMBIN=%~d0\software\pycharm\bin

if not exist %OSGEO4W_ROOT% (
    echo Can't find "%OSGEO4W_ROOT%". Please edit.
    pause
    goto eof
)

set path=%OSGEO4W_ROOT%\apps\qgis\bin;%OSGEO4W_ROOT%\bin;%WINDIR%\system32;%WINDIR%;%WINDIR%\system32\WBem

for %%F in ("%OSGEO4W_ROOT%\etc\ini\*.bat") do call "%%F"

call %OSGEO4W_ROOT%\bin\py3_env.bat
call %OSGEO4W_ROOT%\bin\qt5_env.bat

set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins

REM for QGIS
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH%

call "%PYCHARMBIN%\pycharm.bat %*

EDIT: I too encountered the the SDK-Error, starting PyCharm without a batch file, right from the windows start menue. When it asked for opening/creating a project, i choosed "Create New Project" and then under "Project Interpreter" choosed Existing Interpreter with just pointing to a OSGEO4W-python.exe never used before in pycharm, it shows the SDK-Error. I bet you can delete the bad project and create a new one, starting pycharm with the correct batch file...

Related Question