8

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

3
  • In the working editor do import sys; print(sys.path) What does it say? Commented Jun 27, 2017 at 2:16
  • @StephenRauch my quest is simple i thing but i dont know how to fix it.....i have install 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 dont work,if i change order and is Above then work but recognizes site-pacages from QGIS PYTHON ONLY...that i think is error
    – jessie jes
    Commented Jun 27, 2017 at 17:05
  • @StephenRauch that my export from sys.path in working editor ['', 'C:\\Python27\\Lib\\idlelib', 'C:\\OSGeo4W64\\apps\\qgis-ltr\\python', 'C:\\OSGeo4W64\\apps\\qgis-ltr\\plugins', 'C:\\OSGeo4W64\\apps\\Python27\\Lib\\site-packages', 'C:\\WINDOWS\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages']
    – jessie jes
    Commented Jun 27, 2017 at 19:21

2 Answers 2

2

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.

10
  • can i use Pythonpath from QGIS and work with additional packages from pip in my python27/site-packages ?
    – jessie jes
    Commented Jun 27, 2017 at 20:13
  • Yes. But, note that Python will only use the first module it finds in the path for a given name. Commented Jun 27, 2017 at 20:15
  • what you mean first module ?
    – jessie jes
    Commented Jun 27, 2017 at 20:24
  • The same module name (ie target of an import statement) can appear multiple times in the path. Python will only use the first one found. This means that if you pip install something into the second site packages, any module in the first site packages would not be affected. So, if the first site packages had a module of the same name, then Python would use the module from the first site packages and not the one you just pip installed into the second site packages. This could be confusing. Commented Jun 27, 2017 at 20:29
  • if some package not in the first path but in the second path then that package Is recognized?and if i copy-paste all folders from second place in the first without replace in the some package but just to add new packages in the first path,that is wrong ?thnx you to help me
    – jessie jes
    Commented Jun 27, 2017 at 20:41
0

check the link below, it's not the answer directly, but it provides a tested solution to your problem...

I did this and now my Pycharm understands API autocompletion and everything.

qgis.core import error: no module named qgis.core

and of course here is my interpreter paths in pycharm:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.