Importing one python module loads all others
When a single python module is imported, it attempts vtk attempts to load all of the python modules. If the shared library dependency for any single module is unavailable, this causes the import to fail (even if that module is not being used).
For example, if I try to load vtkCommonCore on a system where liblas is not installed:
Python 3.9.7 (default, Oct 10 2021, 15:13:22)
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import vtk.vtkCommonCore
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.9/site-packages/vtk.py", line 30, in <module>
all_m = importlib.import_module('vtkmodules.all')
File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/usr/lib/python3.9/site-packages/vtkmodules/all.py", line 92, in <module>
from .vtkIOLAS import *
ImportError: liblas.so.3: cannot open shared object file: No such file or directory
>>> from vtk import vtkCommonCore
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.9/site-packages/vtk.py", line 30, in <module>
all_m = importlib.import_module('vtkmodules.all')
File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/usr/lib/python3.9/site-packages/vtkmodules/all.py", line 92, in <module>
from .vtkIOLAS import *
ImportError: liblas.so.3: cannot open shared object file: No such file or directory
This creates a problem for distributions that would like to have optional dependencies for some functionality. For example, liblas is now a required dependency for all installations, even if the end user does not require any of the LAS functionality. The same is the case for other modules that use shared libraries - for example, vtkIOExportPDF or vtkIOODBC. All users would need to install PDF and ODBC libraries, even if they are not using any PDF or ODBC functions.
Maybe this could be solved by some changes to the import logic to avoid unnecessarily loading modules? Or perhaps just some ImportError exception handling in vtkmodules/all.py?