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
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
- Setting up PyCharm for PyQGIS and Qt
- Adding QGIS-specific Python interpreter to Eclipse?
- A Quick Guide to Getting Started with PyQGIS on Windows
- Fixing "The SDK seems invalid" error when setting up PyCharm and PyQGIS?
- Porting Processing scripts to QGIS3
- https://gisforthought.com/updating-a-plugin-from-qgis-2-to-qgis-3/
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:
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:
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...