When running python commands in the QGIS console, you have some neat features: attribute auto complete, syntax highlighting etc.
Naturally, everyone wants to have these features in their IDE of choice.
Let's see how it looks in PyCharm under Debian/Ubuntu.
After you create a new project:
and create a
test.py file with
from qgis.core import QgsDataSourceURI, QgsVectorLayer
you may find out like I did that PyCharm may not recognize it:
You can see from the picture above that we have some red highlights that show that PyCharm is not so happy with what's written there.
By checking the console log of PyCharm I noticed the following:
.. WARN - .skeletons.PySkeletonRefresher - qgis._analysis .. WARN - .skeletons.PySkeletonRefresher - qgis._core .. WARN - .skeletons.PySkeletonRefresher - qgis._gui .. WARN - .skeletons.PySkeletonRefresher - qgis._networkanalysis .. WARN - .skeletons.PySkeletonRefresher - qgis._server
I thought of solving it quickly so I went to
File --> Settings --> Project Interpreter (you can also open this up by doing
CTRL + ALT + S) and add to the interpreter paths the location of the QGIS Python modules which for me is:
Which should look finally like this:
Trying to run the simple import script outputted:
/usr/bin/python2.7 /home/web/PycharmProjects/untitled/test.py Traceback (most recent call last): File "/home/web/PycharmProjects/untitled/test.py", line 1, in <module> from qgis.core import QgsDataSourceURI, QgsVectorLayer File "/home/web/apps/share/qgis/python/qgis/__init__.py", line 36, in <module> from qgis.core import QgsFeature, QgsGeometry File "/home/web/apps/share/qgis/python/qgis/core/__init__.py", line 34, in <module> from qgis._core import * ImportError: libqgis_core.so.2.14.3: cannot open shared object file: No such file or directory
As I was using a QGIS built from source but I hadn't setup globally or locally the path to the QGIS libraries the following step was doing just that.
So, moments later, after closing PyCharm, doing
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/web/apps/lib and reopening it with
./pycharm.sh I ran the script again and the import was successful.
However, for some reason, PyCharm still was not able to build the skeletons of the QGIS modules so the main goal was still unmet.
I tried so many things to no avail among which I can recall:
- setting the PYTHONPATH outside PyCharm
- installing older versions of PyCharm
- clearing the PyCharm cache to force rebuild of skeletons by clicking
File --> Invalidate Caches / Restart..or by deleting the
~/.PyCharm2016.1/and even the
- even went on Windows and see if I encounter the same problem which I did but I solved it rather quickly by following the blogs of Nathan and Gary which I'll sum up towards the end of this page.
If you encounter this kind of problems you better begin and checkout the PyCharm
You can find the its location by going to
Help --> Show Log in Files.
tail -f ~/.PyCharm2016.1/system/log/idea.log proved useful for me as I could see what skeletons are updating and whether the QGIS modules are parsed or not.
Open the Python console from QGIS and run the following:
import sys print(sys.path)
It should output something like:
['/home/web/apps/share/qgis/python/plugins/processing', '/home/web/apps/share/qgis/python', u'/home/web/.qgis2/python', u'/home/web/.qgis2/python/plugins', '/home/web/apps/share/qgis/python/plugins', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/web/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', u'/home/web/.qgis2//python', '/home/web/apps/share/qgis/python/plugins/fTools/tools']
Clean the output by:
- delete the encompassing
Now put it in a PYTHONPATH variable.
The final command should look like
If you built your QGIS from source don't forget to point to the libraries if there's nothing setup by doing
Before testing if it works don't forget to remove whatever skeletons etc. PyCharm did before by removing the
~/.PyCharm2016.1 directory as
File --> Invalidate Caches / Restart.. in PyCharm did jack squat for me.
You can see it works now:
Now, depending on what you want:
- you can make a script that sets the variables and then runs Pycharm
- add the variables to the
- other ..
As I want to have a PyCharm aware QGIS icon on desktop I created a file called
pycharm-qgis-lts.sh in the PyCharm directory with the content described above:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/web/apps/lib export PYTHONPATH=$PYTHONPATH:/home/web/apps/share/qgis/python/plugins/processing:/home/web/apps/share/qgis/python:/home/web/.qgis2/python:/home/web/.qgis2/python/plugins:/home/web/apps/share/qgis/python/plugins:/usr/lib/python2.7:/usr/lib/python2.7/plat-x86_64-linux-gnu:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/home/web/.local/lib/python2.7/site-packages:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PILcompat:/usr/lib/python2.7/dist-packages/gtk-2.0:/home/web/.qgis2//python:/home/web/apps/share/qgis/python/plugins/fTools/tools sh /home/web/Downloads/pycharm/bin/pycharm.sh
The last line executes the
I made the file executable for the owning user with
chmod u+x pycharm-qgis-lts.sh.
In order to also see it as a Desktop application I followed the steps outlined here and I created a
pycharm-qgis-lts.desktop file in the
~/.local/share/applications directory with the content:
[Desktop Entry] Version=1.0 Type=Application Encoding=UTF-8 Name=PyCharm QGIS LTS Icon=/home/web/Downloads/pycharm/bin/pycharm.png Exec="/home/web/Downloads/pycharm/bin/pycharm-qgis-lts.sh" Comment=Develop with pleasure! Categories=Development;IDE; Terminal=false StartupWMClass=pycharm-qgis-lts
qgis_pycharm.bat file with the following content:
@echo off SET OSGEO4W_ROOT=C:\OSGeo4W64 SET PYCHARM="C:\Program Files (x86)\JetBrains\PyCharm 2016.1.4\bin\pycharm.bat" call "%OSGEO4W_ROOT%"\bin\o4w_env.bat call "%OSGEO4W_ROOT%"\apps\grass\grass-7.0.4\etc\env.bat @echo off path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass-7.0.4\lib set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27\Lib\site-packages set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis start "PyCharm aware of QGIS" /B %PYCHARM% %*
You may need to change your paths accordingly but this certainly works. I won't go into details but you can check Nathan's blog or Gary's blog from which I got the main ideas.
Just running the file ensures you'll have a QGIS-modules aware PyCharm.