`import vtk` or `import paraview.vtk`
Any ParaView user (and developer) that is writing Python scripts in ParaView for data processing i.e. for use in Programmable Source or Programmable Filter, will soon notice that there are two ways of importing the vtk package:
# Option 1: import the 'vtk' package directly. import vtk # Option 2: Import the 'vtk' package within 'paraview' from paraview import vtk
In both cases, the vtk package has similar submodules eg. vtk.vtkCommonCore, vtk.vtkCommonDataModel etc. What’s different is which modules are loaded and hence what classes are available within the vtk package itself. To clarify things, let’s look at a simple example:
import vtk vtk.vtkSphereSource() # --- This works!
however the following doesn’t:
from paraview import vtk vtk.vtkSphereSource() # -- This raises 'AttributeError' :(
To get to ‘vtkSphereSource’ from ‘paraview.vtk’, you can use the following syntax:
from paraview.vtk.vtkFiltersSources import vtkSphereSource
Thus, you have to explicitly import the class from the module in which it’s defined.
Now you may be wondering: why this complication?
Simple: since most ParaView programmable filter developers won’t use most of VTK’s Python modules, importing all of them is not only wasteful but also can slow things down considerably, especially when running on large number of MPI ranks in parallel on shared file system.