SimpleITK import error

Hi all,

I’ve been getting this error for a while on mac. This is from the latest nightly. Please let me know if you’d like me to report it on the issue tracker.

Python 3.6.7 (default, Jun 23 2019, 23:06:42) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
>>> import SimpleITK
Traceback (most recent call last):
  File "/Applications/Slicer.app/Contents/lib/Python/lib/python3.6/site-packages/SimpleITK/SimpleITK.py", line 18, in swig_import_helper
    return importlib.import_module(mname)
  File "/Applications/Slicer.app/Contents/lib/Python/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 658, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 571, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 922, in create_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ImportError: dlopen(/Applications/Slicer.app/Contents/lib/Python/lib/python3.6/site-packages/SimpleITK/_SimpleITK.cpython-36m-darwin.so, 1): Library not loaded: /Volumes/Dashboards/Preview/Slicer-0-build/ITK-build/lib/libITKIOBruker-5.0.1.dylib
  Referenced from: /Applications/Slicer.app/Contents/lib/Python/lib/python3.6/site-packages/SimpleITK/_SimpleITK.cpython-36m-darwin.so
  Reason: image not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Applications/Slicer.app/Contents/lib/Python/lib/python3.6/site-packages/SimpleITK/__init__.py", line 1, in <module>
    from .SimpleITK import *
  File "/Applications/Slicer.app/Contents/lib/Python/lib/python3.6/site-packages/SimpleITK/SimpleITK.py", line 21, in <module>
    _SimpleITK = swig_import_helper()
  File "/Applications/Slicer.app/Contents/lib/Python/lib/python3.6/site-packages/SimpleITK/SimpleITK.py", line 20, in swig_import_helper
    return importlib.import_module('_SimpleITK')
  File "/Applications/Slicer.app/Contents/lib/Python/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named '_SimpleITK'
>>> 

Hi @Fernando -

I can replicate your issue on mac with the nightly. The key message seems to be missing libITKIOBruker-5.0.1.dylib.

@blowekamp is coming to project week and hopefully we can look through this together. It seems it’s something to do with the way SimpleITK interacts with remote ITK modules with image IO functions.

1 Like

This indicate that when when the SimpleITK library is being loaded the linker can not find the libITKIOBruker-5.0.1.dylib dependency. The things to check are:

  • Does libITKIOBruker-5.0.1.dylib exist in the proper place in the Mac Bundle?
  • Run otool -L on _SimpleITK.so to verify that it has been fixed up correctly.

Unfortunately, I seem to be having trouble getting to www.slicer.org right now to download the latest nightly. I’ll be at project week on Thursday.

Thanks @blowekamp - slicer.org seems to be working now for me if you want to have a look.

When we looked before it seems the Bruker reader is not being included or fixed up, maybe as part of the SimpleITK build step. Do you know if that’s something that’s changed recently? It works in Slicer 4.10.2 but that’s an older ITK. We can look at it when you are at Project Week.

I was able to download both the nightly and the release versions.

The suffix on the SimpleITK library has changed a bit:

$ find Slicer-4.1* -name _SimpleITK\*
Slicer-4.10.2.app/Contents/lib/Python/lib/python2.7/site-packages/SimpleITK/_SimpleITK.so
Slicer-4.11.0-2019-06-24.app/Contents/lib/Python/lib/python3.6/site-packages/SimpleITK/_SimpleITK.cpython-36m-darwin.so

And the SimpleITK library is not being fixed up at all:

 $otool -L  Slicer-4.11.0-2019-06-24.app/Contents/lib/Python/lib/python3.6/site-packages/SimpleITK/_SimpleITK.cpython-36m-darwin.so

Slicer-4.11.0-2019-06-24.app/Contents/lib/Python/lib/python3.6/site-packages/SimpleITK/_SimpleITK.cpython-36m-darwin.so:
	/Volumes/Dashboards/Preview/Slicer-0-build/ITK-build/lib/libITKIOBruker-5.0.1.dylib (compatibility version 1.0.0, current version 1.0.0)
	/Volumes/Dashboards/Preview/Slicer-0-build/ITK-build/lib/libITKIODCMTK-5.0.1.dylib (compatibility version 1.0.0, current version 1.0.0)
	/Volumes/Dashboards/Preview/Slicer-0-build/ITK-build/lib/libITKIOHDF5-5.0.1.dylib (compatibility version 1.0.0, current version 1.0.0)
	/Volumes/Dashboards/Preview/Slicer-0-build/ITK-build/lib/libITKIOJPEG2000-5.0.1.dylib (compatibility version 1.0.0, current version 1.0.0)
	/Volumes/Dashboards/Preview/Slicer-0-build/ITK-build/lib/libITKIOLSM-5.0.1.dylib (compatibility version 1.0.0, current version 1.0.0)

Ah - I guess it’s a python3 related change?

Here is a PR which should fix it:

I don’t have a Mac setup for Slicer development, so I could test it. This certainly looks like the cause of the SimpleITK library not being fixed up.

1 Like

Thanks @blowekamp for the fix :pray:

It has been integrated in r28347

Unfortunately, the problem persists in today’s nightly.

This PR from @blowekamp should hopefully fix the problem in the next nightly: https://github.com/Slicer/Slicer/pull/1161.

Meanwhile, @jcfr had the idea to install SimpleITK using pip. I first removed the SimpleITK local package:

$ rm -rf ./lib/Python/lib/python3.6/site-packages/SimpleITK*

And then did slicer.util.pip_install("SimpleITK"), but after restarting Slicer, I got a crash, here’s the stack trace.

I think SimpleITK from pip install is v1.2.0 which is built using ITK v4.13 even though it is ITKv5 compatible. Probably not the best since Slicer is using ITKv5

Check with

import SimpleITK as sitk
sitk.Version().ITKVersionString()

Yes, indeed SimpleITK from pip is 4.13.

I thought I asked a question along those lines to JC at the project week, and I thought the response was that packages installed via pip are installed into separate directory, along with their dependencies, so they would not conflict with any packages already installed. I guess I misunderstood the answer. I thought SimpleITK will use ITK it will bring along, and will not conflict with Slicer ITK 5.

I did the following using SlicerPython directly instead of within Slicer and didn’t have any crashing on open.

./SlicerPython.exe -m pip uninstall SimpleITK
Uninstalling SimpleITK-1.2.0rc2.dev413:
  Would remove:
    c:\users\jamesbutler\appdata\local\na-mic\slicer 4.11.0-2019-06-23\lib\python\lib\site-packages\simpleitk
    c:\users\jamesbutler\appdata\local\na-mic\slicer 4.11.0-2019-06-23\lib\python\lib\site-packages\simpleitk-1.2.0rc2.dev413-py3.6.egg-info
Proceed (y / n)? 
  Successfully uninstalled SimpleITK-1.2.0rc2.dev413
./SlicerPython.exe -m pip install SimpleITK
Collecting SimpleITK
  Using cached https://files.pythonhosted.org/packages/ff/b3/2c79f38d113eb3912703261979c88ec1f640488d6d9fac391de42052e8cb/SimpleITK-1.2.0-cp36-cp36m-win_amd64.whl
Installing collected packages: SimpleITK
Successfully installed SimpleITK-1.2.0

There was this warning on open though.

WARNING: In C:\d\VS14-Win64-pkg\SimpleITK-build\ITK\Modules\Core\Common\src\itkObjectFactoryBase.cxx, line 664
Possible incompatible factory load:
Running itk version :
itk version 4.13.1
Loaded factory version:
itk version 5.0.0
Loading factory:
C:/Users/JamesButler/AppData/Local/NA-MIC/Slicer 4.11.0-2019-06-23/bin/../lib/Slicer-4.11/ITKFactories\MRMLIDIOPlugin.dll
1 Like

What happens when you do import SimpleITK in the Slicer python console? I get a crash.

It is also interesting that somehow I am not getting the warning that you mentioned…

I get the output warning (separate window) after importing SimpleITK. Here’s the output I get. No crashing.

>>> import SimpleITK as sitk
>>> sitk.Version().VersionString()
'1.2.0'
>>> sitk.Version().ITKVersionString()
'4.13'

Hmmm… I am on Mac, and installed from this nightly package: Slicer-4.11.0-2019-06-27-macosx-amd64. Is this what you have?

I’m on Windows. I tried again with Slicer-4.11.0-2019-06-27-win-amd64 and didn’t have issues.

1 Like

@jamesobutler thanks a lot for your efforts, I appreciate it!

I will wait for the nightly package, hopefully it works tomorrow.

As I understood from @jcfr, ITK Puthon is built statically, so there is no version conflict except the IO factory. Unfortunately, ABI incompatibility in the IO factory can break everything.

I guess SimpleITK will upgrade to the latest stable ITK version (5.x) at some point, which may happen more quickly if we ask for it.

Unfortunately, as of r28349 the problem is still there.