Missing classes from python module only in PyCharm's editor

 
 

I would like to use PyCharm Community Edition 2020.1.2 x64 on Windows 10 as IDE for writing QGIS plugins ( geospatial information software). I need to base on old version - QGIS 2.18, so I need python 2.7, qt4, and also core modules from QGIS 2.18. I downloaded QGIS 2.18 from official site. It provides a ready to use python environment as .bat script, that sets all needed python paths. So I used path D:\Program Files\QGIS 2.18\bin\python-qgis-ltr.bat to set main python interpreter for my project.

After that I was able to run python console in PyCharm, import all QGIS modules, and any classes from QGIS standard collection like QgsFeature, QgsVectorLayer etc. Everything was fine untill I tried to import this classes from PyCharm editor in .py file as from qgis.core import QgsFeature. In editor PyCharm sees the package qgis.core and I'm able to import it, but PyCharm's code completion inside the module sees only classes that are saved in D:\Program Files\QGIS 2.18\apps\qgis-ltr\python\qgis\core\__init__.py e.g. QgsEditError class, but all standard QGIS class underlines on red.

Python console ran on exactly the same interpreter see the same sources as are visible in "External Libraries" tab in PyCharm, so command:

  • qgis.core.__path__ returns ['D:\\Program Files\\QGIS 2.18\\apps\\qgis-ltr\\python\\qgis\\core'],
  • qgis.core.__file__ returns 'D:\\Program Files\\QGIS 2.18\\apps\\qgis-ltr\\python\\qgis\\core\\__init__.pyc,
  • dir(qgis.core) returns full list of QGIS standard core classes.

If I run the .py script, in which PyCharm's code validator is showing incorrect import object by red underline, the script is executed without any error and I can easly print the imported QgsFeature object. Why my PyCharm's editor see different content of qgis.core module then console ran also from IDE on exactly the same interpreter?

2 comments
Comment actions Permalink

QGIS need to setup an environment before you can use the interpreter properly. On Windows, this can be done by running <QGIS_HOME>/bin/python-qgis.bat

You need that environment in PyCharm. One way to do that is to copy all the environment settings from <QGIS_HOME>/bin/python-qgis.bat to <IDE_HOME>/bin/pycharm.bat

Or just run <QGIS_HOME>/bin/python-qgis.bat in a terminal session, and then run pycharm.bat from this session. I tested and it works.

0
Comment actions Permalink

As I wrote in the first paragraph, use of the <QGIS_HOME>/bin/python-qgis.bat file as an environment for PyCharm was first thing I had tried and it didn't help. Finally, the problem solved itself. It was something wrong with building a binary sceletons of C++ libraries, what PyCharm does every time the application is launched. My PyCharm didn't want to do it and I found many simillar problems with other libriaries (e.g. Qt) that people were struggling with. After two days my PyCharm fixed itself and created all needed binary sceletons for my QGIS libraries..

0

Please sign in to leave a comment.