7

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

4
  • Try to use two batch files, one for python27 and one for python36, they conflict in some environment variables. Mar 16, 2018 at 23:30
  • @AndreasMüller please see my answer below. Mar 19, 2018 at 7:37
  • Sorry, i can't reproduce this error. I'm not using pycharm, although i tried to. It works intransparent for me, i can't figure out, how it works "under the hood" and i don't understand its concepts of Projects and Settings. Recently I used pyzo (which handles different python installs well) and pyScripter (with a new release and better support for python3). Mar 20, 2018 at 15:11
  • With Pyzo, when setting the python interpreter to C:\OSGeo4W64\apps\Python36, I get the error 'The process failed to start (invalid command?). (3221225781)'. With PyScripter, I am not even able to start anything because an error pop's up that there is no Python version on my machine. I think I will just use a QGIS independent Python installation (C:\Python27) to write scripts in PyCharm and then call them in the QGIS 2 or 3 Python console to test them, or code in the script editor of QGIS. Mar 21, 2018 at 8:23

1 Answer 1

4

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...

6
  • This batch file doesn't work for me (a window opens and closes directly, PyCharm is not started). I ran the batch file as it is, only changed system32 to system64. I am not sure if I have to change more. Mar 23, 2018 at 10:52
  • Pycharmbin should also match your Install. Unfortunately we can't debug a batch script. But you can add echo statements and a pause before start to check if everything is ok. Mar 23, 2018 at 11:35
  • Perfect, now it works, thank you! I edited PyCharm bin to 'SET PYCHARMBIN=C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.1\bin' Mar 23, 2018 at 13:21
  • Maybe one more thing to fully answer the question above, where I asked how to configure PyCharm for QGIS Python 2 and 3: I now understood that you have to create two different batch files to start either PyCharm connected to 2 or to 3. To start a PyCharm connected to Python 2, I would remove the call statements to py3_env.bat and qt5_env.bat and qt5 to qt4 in the QT_PLUGIN_PATH. When I start PyCharm with this batch file, I am what to set as 'Python Interpreter'. For Python 3, I set the one under C:\OSGeo4W64\apps\Python36, but in C:\OSGeo4W64\apps\Python27 there is not python.exe Mar 23, 2018 at 14:08
  • Further there is the python.exe and a python3.exe in C:\OSGeo4W64\bin. The former starts a Python 2 shell. However, when defining that as interpreter in PyCharm, an import such 'from qgis.core import *' starts looking for PyQt5.QtCore and throws errors. Mar 23, 2018 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.