Sharing python code between (scripted loadable) modules

Hey,
I should chime-in since you referenced a topic I posted previously. In my case I have a custom library that was shared by two separate applications - a TCP/IP socket class. Let’s call it sock.py
to use sock.py in a slicer scripted module I had to setup the directory structure as so

WidgetRootDir
 |
 | - slicerWidget.py
 | - subdirectory
      | - __init__.py
      | - sock.py

__init__.py contains

from .sock import *

And slicerWidget.py imports as so

from subdirectory import sock

If you need to access another slicer module this is a clean way of doing so Setting widget variable from --python-code on Slicer.exe start - #13 by talmazov

I hope this helps

~ Georgi

1 Like