Python plugin split into multiple modules

I am using ParaView 5.13 on linux and I would like to create a Python plugin which consists of multiple modules. The purpose is to make the structure better organized and to be able to reuse the different modules in other plugins in future. I have a main script called SwirlingStreamlines.py and I would like to import a module called SeedPoints.py. I am using the command import SeedPoints in SwirlingStreamlines.py. When I try to load the plugin into ParaView, I am getting the following error:

Traceback (most recent call last):
  File "/home/marek/paraview_build/lib/python3.10/site-packages/paraview/detail/pythonalgorithm.py", line 538, in load_plugin
    module = imp.load_source(modulename, filepath)
  File "/home/marek/anaconda3/lib/python3.10/imp.py", line 172, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 719, in _load
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/marek/Workspace/ParaviewPlugins/Python/VortexIdentification/SwirlingStreamlines.py", line 13, in <module>
    import SeedPoints
ModuleNotFoundError: No module named 'SeedPoints'
(   1.187s) [paraview        ]  vtkPVPluginLoader.cxx:670    ERR| vtkPVPluginLoader (0x55aedfb2bb60): /home/marek/Workspace/ParaviewPlugins/Python/VortexIdentification/SwirlingStreamlines.py: /home/marek/Workspace/ParaviewPlugins/Python/VortexIdentification/SwirlingStreamlines.py: invalid ELF header

I think there are some path issues to solve: the directory containing your main .py is not added to the PYTHONPATH so “relative” imports are not possible.

A workaround I see is something like the following:

Main directory:

SwirlingStreamlines.py
MyModule
 |_ SeedPoints.py

SwirlingStreamLines.py

import os,sys
sys.path.append(os.path.dirname(__file__))

from MyModule import SeedPoints

Thank you Nicolas, the proposed solution works!

1 Like