4

Question

I'm trying to figure what are the differences, and of course, what is the best solution to use, between two python3 bindings:

  • GDAL, imported as import gdal
  • OSGEO.GDAL, imported as from osgeo import gdal

Indeed, here are the results of some commands such as .__file__ and .__version__ methods for both of them;

$ python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.    

>>> import gdal
>>> gdal.__file__
'/usr/local/lib/python3.6/dist-packages/gdal.py'
>>> gdal.__version__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'gdal' has no attribute '__version__'    

>>> from osgeo import gdal
>>> gdal.__file__
'/usr/local/lib/python3.6/dist-packages/osgeo/gdal.py'
>>> gdal.__version__
'2.4.0'
>>> 

Environment and informations

Ubuntu 5.0.0-37-generic x86_64 GNU/Linux
Python 3.6.9 (default, Nov 7 2019, 10:44:02)

$ pip3 list | grep -i gdal
GDAL                          2.4.0
$ pip3 list | grep -i osgeo
(empty result)
$ apt-cache search gdal | grep -i python
python-rasterio - Python 2 API for using geospatial raster data with Numpy
python-stetl - Streaming ETL - Geospatial ETL framework for Python 2
python3-csvkit - public modules for csvkit (Python 3)
python3-rasterio - Python 3 API for using geospatial raster data with Numpy
python3-gdal - Python 3 bindings to the Geospatial Data Abstraction Library
python-gdal - Python bindings to the Geospatial Data Abstraction Library
$ apt-cache search osgeo
python-geolinks - Python 2 Library for using geospatial links (catalogue interoperablity)
python3-geolinks - Python 3 Library for using geospatial links (catalogue interoperablity)
pycsw - OGC compliant metadata (Catalogue Service for the Web) server
pycsw-doc - OGC compliant metadata (Catalogue Service for the Web) server - documentation
pycsw-wsgi - WSGI Apache CSW service based on pycsw
python-pycsw - OGC compliant metadata (Catalogue Service for the Web) server - Python 2
python-pycsw-doc - Transitional dummy package to pycsw-doc
python-pycsw-wsgi - Transitional dummy package to pycsw-wsgi
python3-pycsw - OGC compliant metadata (Catalogue Service for the Web) server - Python 3

I don't know from where do both packages come from at this point (e.g. which was installed by apt or pip).

Links

Difference between osgeo gdal/ogr python module and GDAL C++ library
https://gdal.org/
https://www.osgeo.org/

6

As documented in https://pypi.org/project/GDAL/ plain "gdal" is deprecated and you should not use that for writing any new code

Additionally, there are five compatibility modules that are included but provide notices to state that they are deprecated and will be going away. If you are using GDAL 1.7 bindings, you should update your imports to utilize the usage above [from osgeo import gdal], but the following will work until at least GDAL 2.1.

import gdal

import ogr

import osr

import gdalnumeric

import gdalconst

Since GDAL 2.0, published in 2015, the right way to import is

>>> from osgeo import gdal
>>> from osgeo import ogr
>>> from osgeo import osr
>>> from osgeo import gdal_array
>>> from osgeo import gdalconst
| improve this answer | |
  • Thanks. Could you please append to your answer the correct way (in the yellow box is the "incorrect" way (with any modern gdal version)) to import the gdal familiy package? This is for more clarity for future visitors. – s.k Jan 15 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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