[QGIS-Developer] Best way to import gdal, ogr, osr ?

Raymond Nijssen r.nijssen at terglobo.nl
Mon Oct 17 01:52:48 PDT 2022


Dear developers,

What is the best way to import ogr, gdal and osr in my pyqgis script?


This used to work in older QGIS versions:

import gdal
import ogr
import osr



Now I've changed that to:

try:
     import ogr
     import gdal
     import osr
except ModuleNotFoundError:
     from osgeo import ogr
     from osgeo import gdal
     from osgeo import osr


But it's still unclear to me what would be the way to make sure my 
script will run in anyone else's installation (Different OS and QGIS 
version).


Maybe there's something like this:

from qgis import ogr

???


Kind regards,
Raymond


More information about the QGIS-Developer mailing list