Otbenv.profile causes problems with gdal_*.py scripts

Before running
. /home/alobo/OTB/OTB-8.0.1-Linux64/otbenv.profile
I get:

$ gdal_polygonize.py WS1000_00001.tif WS1000_00001.shp
Creating output WS1000_00001.shp of format ESRI Shapefile.
0...10...20...30...40...50...60...70...80...90...100 - done.


$ . /home/alobo/OTB/OTB-7.3.0-Linux64/otbenv.profile

I get:

$ gdal_polygonize.py WS1000_00001.tif test.shp
Traceback (most recent call last):
  File "/home/alobo/OTB/OTB-7.3.0-Linux64/bin/gdal_polygonize.py", line 5, in <module>
    from osgeo.utils.gdal_polygonize import *  # noqa
ModuleNotFoundError: No module named 'osgeo'


$ . /home/alobo/OTB/OTB-8.0.1-Linux64/otbenv.profile

I get

$ gdal_polygonize.py WS1000_00001.tif test.shp
bash: /home/alobo/OTB/OTB-8.0.1-Linux64/bin/gdal_polygonize.py: /opt/rh/rh-python38/root/usr/bin/python3.8: bad interpreter: No such file or directory

This causes problems in i.e. some QGIS apps (eg., Polygonize…)
Any way to avoid it? (besides not including otbenv.profile in the users’s .profile)

Hi @alobo

This is a known problem, you can add a gdal-config file pointing to the right gdal version because when you source the otbenv.profile the system gdal-config points to the wrong one. More informations here :

Let me know if it works after that
Best Regards

No, it does not work.
What I (barely) understand from the link is that I must run:

Once I do it, I get a line added to otbenv.profile:

But I run

. /home/alobo/OTB/OTB-8.0.1-Linux64/otbenv.profile

and then I get the mentioned error (which I do not get before running otbenv.profile):

$ gdal_polygonize.py WS1000_00001.tif test.shp
bash: /home/alobo/OTB/OTB-8.0.1-Linux64/bin/gdal_polygonize.py: /opt/rh/rh-python38/root/usr/bin/python3.8: bad interpreter: No such file or directory

I guessed that the previous step:

For that purpose you can drop this simplified and generic version of gdal-config into the bin/ directory where you’ve extracted OTB. This will permit pip install gdal==vernum to work correctly.

does not apply in my case, but I might be wrong as I do not quite understand what I’m doing.

Anyway, adding the gdal-config file does not solve the problem either.
Also, if the problem is known and you have a solution, why is not applied by the installation script?

Your problem with OTB-8.0.1 install is that you have not recompiled your python bindings, so the script gdal_polyginize is looking for the python installation used to build the binary package.
Recompile the python bindings and let me know if it works better.

gdal-config is not incorporated yet during the installation because it is used for remote modules only. We are looking for the best solution before providing it widely.
Another explanation is that from gdal 3.2 the scripts gdal_polygonyze etc have been deprecated and moved to python osgeo.utils/gdal directory, the gdal_polygonyze.py is now a deprecated script but in the OTB install we make a link to the osgeo.utils/gdal to keep backward compatibility. In the GDAL news page this is explained that to use those scripts you now have to import directly osgeo.utils import gdal and do your stuff.

Best regards

Excuse my ignorance, what is “Recompile the python bindings”? How do I do that?

You need to recompile the python bindings when you use another version than CentOS7 Python 3.8. IE if you are on Ubuntu, you have python 3.8 but all the python scripts in the OTB installation still try to use the RH Python 3.8. In order to use yours, you have to recompile the python bindings.


Here you have a tutorial to install dependencies if missing, and then you have the section “recompile the python bindings”

Solved after

source otbenv.profile
ctest -S share/otb/swig/build_wrapping.cmake -VV

that ends with:

Command exited with the value: 0
MakeCommand:/usr/bin/cmake --build . --config “Release” --target “install” – -i
0 Compiler errors
6 Compiler warnings

as instructed in Installation — Orfeo ToolBox 8.0.1 documentation section Linux/Python bindings.