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: