I'm actually new to Python and QGIS. I'm using PyCharm as my code editor. I'm trying to load a raster layer in the following below:

import os
import qgis.core
from osgeo import gdal,gdal_array
from qgis.utils import iface

os.chdir ("C:/Users/User/Desktop/Ih_HowtoCalculateVegetationIndexNDVIfromSentinel2withPyQGIS/Ih_HowtoCalculateVegetationIndexNDVIfromSentinel2withPyQGIS/Sentinel2Clip")
SWIR = iface.addRasterLayer('clip_RT_S2A_OPER_MSI_L1C_TL_MTI__20160506T214824_A004555_T18LTM_B11.tif','SWIR')
NIR = iface.addRasterLayer('clip_RT_S2A_OPER_MSI_L1C_TL_MTI__20160506T214824_A004555_T18LTM_B08.tif','NIR')

but I'm getting the following an attribute error in the code.

C:\OSGeo4W64\bin\python3.exe C:/Users/User/PycharmProjects/unit/proj.py

  File "C:/Users/User/PycharmProjects/unit/proj.py", line 9, in <module>
    SWIR = iface.addRasterLayer('clip_RT_S2A_OPER_MSI_L1C_TL_MTI__20160506T214824_A004555_T18LTM_B11.tif','SWIR')
AttributeError: 'NoneType' object has no attribute 'addRasterLayer'

Process finished with exit code 1

1 Answer 1


When you run your code within PyCharm, you are in a QGIS standalone application case. iface is never available in standalone QGIS application, only within a running QGIS.

If you do from qgis.utils import iface and then print(iface), you get None. You are asking a method addRasterLayer on a None object (try running None.addRasterLayer ;) ), hence the error you mentioned

AttributeError: 'NoneType' object has no attribute 'addRasterLayer'

Look at recipes to use QGIS as standalone application.

  • Thanks so much Mr Thomas. As I said ealier I'm new to both QGIS and python hence I'm so confuse looking at the recipes. I don't really know where to start from. can you please help me out? Thanks. Commented Jun 13, 2021 at 11:48

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.