VTK module import error

Hi All,

I am trying to run the following example (K3D-jupyter/volume_renderer.ipynb at 3fef1ea419a84a51c8aed56cb285b9fcf0a18c9a · K3D-tools/K3D-jupyter · GitHub) in jupyter notebook.

import k3d
import math
import numpy as np
import nibabel as nib
from k3d.helpers import download
import ipywidgets as widgets
import vtk
from vtkmodules.util import numpy_support
basic_color_maps = [(attr, getattr(k3d.basic_color_maps, attr)) for attr in dir(k3d.basic_color_maps) if
                    not attr.startswith('__')]
paraview_color_maps = [(attr, getattr(k3d.paraview_color_maps, attr)) for attr in dir(k3d.paraview_color_maps) if
                       not attr.startswith('__')]
matplotlib_color_maps = [(attr, getattr(k3d.matplotlib_color_maps, attr)) for attr in dir(k3d.matplotlib_color_maps)
                         if not attr.startswith('__')]
colormaps = basic_color_maps + paraview_color_maps + matplotlib_color_maps

filename = download('https://vedo.embl.es/examples/data/embryo.slc')
reader = vtk.vtkSLCReader()
vti = reader.GetOutput()

bounds = vti.GetBounds()
x, y, z = vti.GetDimensions()
img = numpy_support.vtk_to_numpy(vti.GetPointData().GetArray(0)).reshape(-1, y, x)

tf_editor = k3d.transfer_function_editor()
volume = k3d.volume(img.astype(np.float16))

@widgets.interact(x=widgets.Dropdown(options=colormaps, description='ColorMap:'))
def g(x):
    tf_editor.color_map = np.array(x, dtype=np.float32)

plot = k3d.plot()
plot += volume

a = widgets.jslink((tf_editor, 'color_map'), (volume, 'color_map'))
a = widgets.jslink((tf_editor, 'opacity_function'), (volume, 'opacity_function'))

and I get the following error

ImportError                               Traceback (most recent call last)
~\anaconda3\lib\site-packages\vtkmodules\vtkIOImage.py in <module>
      4     # use relative import for installed modules
----> 5     from .vtkIOImagePython import *
      6 except ImportError:

ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-5-b5758b6de607> in <module>
      5 from k3d.helpers import download
      6 import ipywidgets as widgets
----> 7 import vtk
      8 from vtkmodules.util import numpy_support
      9 basic_color_maps = [(attr, getattr(k3d.basic_color_maps, attr)) for attr in dir(k3d.basic_color_maps) if

~\anaconda3\lib\site-packages\vtk.py in <module>
     30     all_spec = importlib.util.find_spec('vtkmodules.all')
     31     all_m = importlib.util.module_from_spec(all_spec)
---> 32     all_spec.loader.exec_module(all_m)
     34     # import vtkmodules

~\anaconda3\lib\site-packages\vtkmodules\all.py in <module>
     54 from .vtkFiltersTopology import *
     55 from .vtkFiltersVerdict import *
---> 56 from .vtkIOImage import *
     57 from .vtkImagingHybrid import *
     58 from .vtkInfovisLayout import *

~\anaconda3\lib\site-packages\vtkmodules\vtkIOImage.py in <module>
      7     # during build and testing, the modules will be elsewhere,
      8     # e.g. in lib directory or Release/Debug config directories
----> 9     from vtkIOImagePython import *

ModuleNotFoundError: No module named 'vtkIOImagePython'

I checked the files available in vtkmodules folder in the anaconda environment


and I could only find the vtkIOImagePython.pyd file and not vtkIOImagePython.py.

I’m using Windows.

I am not sure how to solve this error. Suggestions will be really helpful.

I also checked this post VTK Module Import Error | Anaconda Windows 10 - ✉️ Mailing List Archive (read-only) - MNE Forum. But it’s not clear to me how the module import error can be fixed.
Thanks a lot for your time and kind attention.

Any suggestions?


ImportError: DLL load failed: The specified module could not be found.

indicates that it’s unable to find a .dll file that is being used by a python module it is importing. You likely need to add the directory containing the VTK .dlls. Unfortunately I don’t know how you might add directories to the path in anaconda/jupyter environment.

Thank you, I will give it a try.

Could you please let me know where I can find the VTK .dlls ?

How did you install VTK? With pip or conda or something else?

Thanks for the response. I installed via pip

What’s your version of python and which version of VTK was installed?

python --version
pip show vtk

Python 3.7.9

Name: vtk
Summary: VTK is an open-source toolkit for 3D computer graphics, image processing, and visualization
Location: c:\users\xxx\anaconda3\lib\site-packages
Required-by: mayavi, vedo

I’m now using

Python 3.9.15

Name: vtk Version: 9.2.2 Summary: VTK is an open-source toolkit for 3D computer graphics, image processing, and visualization Home-page: [https://vtk.org](https://vtk.org/) Author: VTK developers Author-email: License: BSD Location: c:\users\xxx\anaconda3\envs\differential_networks\lib\site-packages Requires: matplotlib, wslink Required-by: vedo.

I don’t get the module import error now. But I see the following error

AttributeError                            Traceback (most recent call last)
Input In [1], in <cell line: 25>()
     23 bounds = vti.GetBounds()
     24 x, y, z = vti.GetDimensions()
---> 25 img = numpy_support.vtk_to_numpy(vti.GetPointData().GetArray(0)).reshape(-1, y, x)
     27 tf_editor = k3d.transfer_function_editor()
     28 volume = k3d.volume(img.astype(np.float16))

File ~\anaconda3\envs\differential_networks\lib\site-packages\vtkmodules\util\numpy_support.py:215, in vtk_to_numpy(vtk_array)
    200 def vtk_to_numpy(vtk_array):
    201     """Converts a VTK data array to a numpy array.
    203     Given a subclass of vtkDataArray, this function returns an
    214     """
--> 215     typ = vtk_array.GetDataType()
    216     assert typ in get_vtk_to_numpy_typemap().keys(), \
    217            "Unsupported array type %s"%typ
    218     assert typ != vtkConstants.VTK_BIT, 'Bit arrays are not supported.'

AttributeError: 'NoneType' object has no attribute 'GetDataType'

3.7 is before os.add_dll_directory which was added in 3.8 to help with this problem on Windows.