0

I recently had to reinstall my conda environment on Linux. I installed from my old environment.yml file, and it picked up the following gdal versions:

$ conda list|grep gdal
gdal                      3.2.1            py38hc0b2d6b_7    conda-forge
libgdal                   3.2.1                h38ff51b_7    conda-forge

These is fairly recent, but in 3.2.0, import as gdal was removed

Python bindings: old-style "import gdal" is no longer available.
Use "from osgeo import gdal" instead

I have tons of code that use import gdal, and it's just time consuming catching all instances import gdal and change them to from osgeo import gdal. Is there some way to automate this (other than using find and editing things by hand?)

4
  • 1
    sed 's/import gdal/from osgeo import gdal/'
    – user30184
    Apr 7, 2021 at 11:43
  • In hindsight, this is what I was trying to avoid: some packages are installed locally, some for some environments, some are being developed... So I'd rather have to go fixing all these issues one by one.
    – Jose
    Apr 7, 2021 at 13:23
  • Do you believe there is something that prevents the mass edit? Syntax from osgeo import gdal has been supported for years.
    – user30184
    Apr 7, 2021 at 13:30
  • 1
    Check this out, it may help ;-) : Differences between OSGEO GDAL and GDAL python bindings? Apr 7, 2021 at 13:54

1 Answer 1

1

The easiest hack is to add the gdal.py scripts (and ogr.py and osr.py) convenience imports from the historic GDAL distribution somewhere towards the top of the PYTHONPATH.

This is a better solution for multi-user systems than editing all the source code, as if this invocation of gdal is common throughout complex codebases, it's hard to find all occurrences.

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.