PyQGIS Error – Resolving ImportError: No Module Named qgis.core on Windows 10 for Successful QGIS Setup

errorimporterrorpyqgiswindows 10

I am trying to execute the following code on my Windows 10 with QGIS 2.18.20.

from qgis.core import *
from qgis.utils import *
import os, sys

QgsApplication.setPrefixPath("C:/Program Files/QGIS 2.18/apps/qgis-ltr", True)
app = QApplication([], True)
QgsApplication.initQgis()

sys.path.append('C:/Program Files/QGIS 2.18/apps/qgis-ltr')
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *

path_to_layers = "path"

gebaude = path_to_layers + "layer1.shp"
duek = path_to_layers + "layer2.shp" 
ergebnisse = path_to_layers + "result.shp"

general.runalg("qgis:joinattributesbylocation", gebaude, duek, ['intersects','overlaps','within'], 0, 0, 'sum', 1, ergebnisse)

print("Finished with no issues")

However, I am encountering the error

Traceback (most recent call last):
  File "path_to_the_python_code\Gebaeude.py", line 1, in <module>
    from qgis.core import *
ImportError: No module named qgis.core

Have already seen similar threads on this topic but can not really understand how and where I should adjust the full path to the Python executable under the QGIS installation.

set PYTHONPATH="C:/Program Files/QGIS 2.18/apps/Python27"
set PATH="C:/Program Files/QGIS 2.18/bin"

I tried the above set-ups in OSGeo4W Shell through the bat file but no success yet. What I am doing wrong?

OSGeo4W_Shell_Result


References:

Best Answer

Firstly, I see you have a standalone install of QGIS; my answer is based on an OSGeo4W install (it's been a while since I used the standalone installers) but I think the approach should be much the same.

If you just want to use a python shell, you can make a copy of your osgeo4w.bat file in the same location and rename it (like osgeo4w_pyqgis.bat) or whatever you like...

Open it in a text editor, find the line which calls o4w_env.bat, just replace o4w_env.bat with python-qgis-ltr.bat and save.

So mine (for 2.18) would look like this:

@echo off
rem Root OSGEO4W home dir to the same directory this script exists in
call "%~dp0\bin\python-qgis-ltr.bat"

rem List available o4w programs
rem but only if osgeo4w called without parameters
@echo on
@if [%1]==[] (echo run o-help for a list of available commands & cmd.exe /k) else (cmd /c "%*")

When you double click on this batch file it should open you up directly to a python prompt with environment variables correctly set. You can test this by typing from qgis.core import * and hitting enter to make sure you don't get any errors.

Typing quit() or exit() once will take you back to an osgeo4w shell.

Another approach is to use a batch file like this:

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W64
SET QGISNAME=qgis-ltr
SET QGIS=%OSGEO4W_ROOT%\apps\%QGISNAME%
SET QGIS_PREFIX_PATH=%QGIS%

CALL %OSGEO4W_ROOT%\bin\o4w_env.bat

SET PATH=%PATH%;%QGIS%\bin
SET PYTHONPATH=%QGIS%\python;%PYTHONPATH%

cmd.exe

Again this is for an OsGeo4W install; for a standalone install, I think you would just have to change this line:

SET OSGEO4W_ROOT=C:\OSGeo4W64

Not 100% sure, but I think setting osgeo4w_root to C:\Program Files\QGIS 2.18 should work.

The advantage of this batch file is that you can change the last line to start an IDE such as PyCharm with environment variables correctly set for pyqgis development.