[GIS] Defining PyCharm to understand PyQGIS and GDAL

installationpycharmpyqgispython-2.7

I use PyQGIS in standalone apps and I have defined paths in Windows and Python paths to take Python API from outside QGIS.
I have created some simple tools using PyQGIS and GDAL in standalone script using Python editor and all work fine without error qgis.core and GDAL work nice.

Now I want to use PyCharm editor and I have big problems.

First, it shows me two interpreters python.exe and bin/python.exe
but have some paths.

interpreter

python interpreter

qgis

Now in PyCharm if I use python.exe, it shows me dll error in GDAL and qgis.core. If i use Python from QGIS then I have imports error in other packages i need.

Why does it do that?

In Python, IDLE works fine.

Here the windows define paths :

pythonpath

C:\OSGeo4W64\apps\qgis-ltr\python
C:\OSGeo4W64\apps\qgis-ltr\plugins
C:\OSGeo4W64\apps\Python27\Lib\site-packages

path

C:\OSGeo4W64\bin
C:\OSGeo4W64\apps\qgis-ltr\bin
C:\OSGeo4W64\share\gdal
C:\Python27\
C:\Python27\Scripts

I think so the problems is the paths.


I have installed Python version 2.7.13 QGIS have his python… if in the define paths QGIS python path is below from PYTHON path then QGIS Python API don't work, if I change order and is above then work but recognizes site-packages from QGIS PYTHON ONLY…that I think is error

Best Answer

QGis has installed it's own version of Python. So you have two installations. The PYTHONPATH is used to specify which directories to scan for source modules. In this case PyCharm appears to be confused as to which installation to use. But you should be able to configure it as needed.

sys.path is initialized from PYTHONPATH

Both versions of your PyCharm paths, when compared to your working list, are missing:

C:\\Python27\\Lib\\idlelib
C:\\WINDOWS\\SYSTEM32\\python27.zip
C:\\Python27\\lib\\plat-win

You should change the Interpreter Paths in PyCharm to match the working list.

Testing paths in PyCharm

The short program:

import sys
for p in sys.path:
    print(p)

Can be used to verify that all of the paths needed by your program are available in your PyCharm environment.

Related Question