Try to load OTB bindings or scan system, help user in case of failure, set env.
If in interactive prompt, user will be asked if he wants to install OTB.
The OTB_ROOT variable allow one to override default OTB version, with auto env setting.
Path precedence : $OTB_ROOT > location of python bindings location
Then, if OTB is not found:
search for releases installations: $HOME/Applications
OR (for Linux): /opt/otbtf > /opt/otb > /usr/local > /usr
OR (for Windows): C:/Program Files
| Parameters: |
-
prefix
(str, default:
OTB_ROOT
)
–
prefix to search OTB in (Default value = OTB_ROOT)
-
scan
(bool, default:
True
)
–
find otb in system known locations (Default value = True)
|
| Raises: |
-
SystemError
–
is OTB is not found (when using interactive mode)
-
SystemExit
–
if OTB is not found, since pyotb won't be usable
|
Source code in pyotb/helpers.py
| def find_otb(prefix: str = OTB_ROOT, scan: bool = True):
"""Try to load OTB bindings or scan system, help user in case of failure, set env.
If in interactive prompt, user will be asked if he wants to install OTB.
The OTB_ROOT variable allow one to override default OTB version, with auto env setting.
Path precedence : $OTB_ROOT > location of python bindings location
Then, if OTB is not found:
search for releases installations: $HOME/Applications
OR (for Linux): /opt/otbtf > /opt/otb > /usr/local > /usr
OR (for Windows): C:/Program Files
Args:
prefix: prefix to search OTB in (Default value = OTB_ROOT)
scan: find otb in system known locations (Default value = True)
Returns:
otbApplication module
Raises:
SystemError: is OTB is not found (when using interactive mode)
SystemExit: if OTB is not found, since pyotb won't be usable
"""
otb = None
# Try OTB_ROOT env variable first (allow override default OTB version)
if prefix:
try:
set_environment(prefix)
import otbApplication as otb # pylint: disable=import-outside-toplevel
return otb
except SystemError as e:
raise SystemExit(f"Failed to import OTB with prefix={prefix}") from e
except ImportError as e:
__suggest_fix_import(str(e), prefix)
raise SystemExit("Failed to import OTB. Exiting.") from e
# Else try import from actual Python path
try:
# Here, we can't properly set env variables before OTB import.
# We assume user did this before running python
# For LD_LIBRARY_PATH problems, use OTB_ROOT instead of PYTHONPATH
import otbApplication as otb # pylint: disable=import-outside-toplevel
if "OTB_APPLICATION_PATH" not in os.environ:
lib_dir = __find_lib(otb_module=otb)
apps_path = __find_apps_path(lib_dir)
otb.Registry.SetApplicationPath(apps_path)
return otb
except ImportError as e:
pythonpath = os.environ.get("PYTHONPATH")
if not scan:
raise SystemExit(
f"Failed to import OTB with env PYTHONPATH={pythonpath}"
) from e
# Else search system
logger.info("Failed to import OTB. Searching for it...")
prefix = __find_otb_root()
# Try auto install if shell is interactive
if not prefix and hasattr(sys, "ps1"):
raise SystemError("OTB libraries not found on disk. ")
if not prefix:
raise SystemExit(
"OTB libraries not found on disk. "
"To install it, open an interactive python shell and 'import pyotb'"
)
# If OTB was found on disk, set env and try to import one last time
try:
set_environment(prefix)
import otbApplication as otb # pylint: disable=import-outside-toplevel
return otb
except SystemError as e:
raise SystemExit("Auto setup for OTB env failed. Exiting.") from e
# Help user to fix this
except ImportError as e:
__suggest_fix_import(str(e), prefix)
raise SystemExit("Failed to import OTB. Exiting.") from e
|