Sunday, 6 August 2017

pyqgis - Understanding qgis.core import error: no module named qgis.core?


I searched all the same questions, and none of them works for me, so i decided to ask this question in addition to those:


I installed QGIS 2.18 via this downloaded setup 'QGIS-OSGeo4W-2.18.11-1-Setup-x86_64.exe'.



and my PATH system variables are :



C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;%PYTHONHOME%;%PYTHONHOME%\Scripts;C:\Program Files\QGIS 2.18\bin;%PATH%



my PATH user variables:



C:\Users\ash\AppData\Local\Programs\Python\Python36-32\Scripts\;C:\Users\ash\AppData\Local\Programs\Python\Python36-32\



PYTHONHOME :




C:\Program Files\QGIS 2.18\apps\Python27



PYTHONPATH :



C:\Program Files\QGIS 2.18\apps\Python27\Lib



*** I use pycharm and my project interpreter is :



Python 2.7.5 (C:\Program Files\QGIS 2.18\bin\python.exe




now if I import qgis.core it will give



import error: no module named qgis.core



error in pycharm, but no errors in qgis python console!!!


What did I do wrong?



Answer



I found it, it was just a small tricky approach.


in OsGeo4w website says you have to install QGIS by Osgeo4w setup which it sets all kinds of path variables for you ...


( it was NOT entirely true, in my case)



leave your PATH, Path, PYTHONPATH, PYTHONHOME to where it's functional for all modules unless qgis's.


I removed QGIS standalone and installed QGIS via OsGeo4w however. so the path variables that I wrote in the question must change to the same directories but NOT in QGIS 2.18 ROOT, rather in C:\OSGeo4W64.


for example, my PYTHONHOME is now :



C:\OSGeo4W64\apps\Python27



and so on...


so I created a whatever.cmd file using notepad, with this information in it :



@echo off



SET OSGEO4W_ROOT=C:\OSGeo4W64


call "%OSGEO4W_ROOT%"\bin\o4w_env.bat


call "%OSGEO4W_ROOT%"\apps\grass\grass-6.4.3\etc\env.bat


@echo off


path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin


path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass-6.4.3\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


set PATH=C:\Program Files\JetBrains\PyCharm Community Edition 2017.1.4\bin;%PATH%



start "PyCharm aware of Quantum GIS" /B "C:\Program Files\JetBrains\PyCharm Community Edition 2017.1.4\bin\pycharm64.exe" %*



and saved it to somewhere.


So if you use Qgis standalone just instead of osgeo_root write your qgis_root directory.


the few last line are dedicated to familiarizing all this mess to Pycharm.


be careful of typing mistakes. I learned, one wrong char can mess it up...


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...