Import and use existing code/library in a new module

Hi,

in a previous work, I made a c++ library usable from a python program (using the cython tool). It works perfectly but has to be run from the command line.

Now I’m trying to develop a module that would allow to launch automatically some features of this library from the Slicer interface.

I manage to import the python code of my library (by adding the path in sys.path) but this code imports a .so/.pyx library compiled with cython and which does not work. (I don’t know if I’m making myself understood …)

The console error when I start Slicer :

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/llafitte007/Slicer-4.11.20210226-linux-amd64/lib/Python/lib/python3.6/imp.py", line 170, in load_source
    module = _exec(spec, sys.modules[name])
  File "<frozen importlib._bootstrap>", line 618, in _exec
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/llafitte007/__CODE__/primetimeire/SlicerExtensions/Primetime/NeedlesCoordinates/NeedlesCoordinates.py", line 23, in <module>
    from NeedlesExtraction import NeedlesExtraction, PrimetimeConfiguration, Image
  File "/home/llafitte007/__CODE__/primetimeire/NeedlesExtraction.py", line 6, in <module>
    from wrappers import PyIrenaUtils
  File "/home/llafitte007/__CODE__/primetimeire/wrappers/__init__.py", line 1, in <module>
    from .PyIrena import PyIrena, PyIrenaPatient, PyIrenaUtils
  File "/home/llafitte007/__CODE__/primetimeire/wrappers/PyIrena/__init__.py", line 8, in <module>
    from PyIrena import PyIrena, PyIrenaPatient, PyIrenaUtils, ptype, ConfigKey
ModuleNotFoundError: No module named 'PyIrena'

Someone could help me ?

To resume, I tried to use a .so/.pyx library imported from a file I imported in a Slicer module … :face_with_raised_eyebrow:

I tried but it doesn’t work and I can’t show you the error… because nothing happens !

Slicer tries to open (loading window) then nothing : no error in the terminal, nothing !

I continue the investigation and I will keep you informed

After several tests, it seems when Slicer started and load the library this makes it crash (absolutely no errors in console)

I tried manually it in base python console and it’s work
I tried manually in slicer python console and it makes crash slicer without output errors.

Any idea what could cause slicer to crash when loading a library?

edit : can it be due to a different version of python between slicer (3.6) and during the build of the lib (3.8) ?

This is the thing. Try using the last version of slicer with python 3.9 and please try to compile your library also in python 3.9

After recompile the library with python 3.9 and use Slicer 5.1 (also with python3.9) all work fine.

Also, for information with python3.9 there is a clear warning message when loading a library with wrong version python.

1 Like