SuperElastix / SimpleElastix

Multi-lingual medical image registration library
http://simpleelastix.github.io
Apache License 2.0
509 stars 149 forks source link

How to choose python interpreter version for make process? #429

Closed TaWald closed 3 years ago

TaWald commented 3 years ago

Hello, when I follow the installation instructions from the docs "Compiling on Linux" I run into some issues. Sidenote: I run Ubuntu 20.04 with python2.7, python3.6/3.8/3.9 installed. My default python version refers to python2.7.

After building with cmake and running python Packaging/setup.py install successfully (with any python3 virtualenv) I open the virtualenv's python and try to call import SimpleITK.

This results in the following error:

File "/home/tassilowald/Code/build/SimpleITK-build/Wrapping/Python/SimpleITK.py", line 14, in swig_import_helper
    return importlib.import_module(mname)
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 565, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 1108, in create_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
ImportError: /home/tassilowald/Code/build/SimpleITK-build/Wrapping/Python/_SimpleITK.so: undefined symbol: PyClass_Type

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/tassilowald/Code/build/SimpleITK-build/Wrapping/Python/SimpleITK.py", line 17, in <module>
    _SimpleITK = swig_import_helper()
  File "/home/tassilowald/Code/build/SimpleITK-build/Wrapping/Python/SimpleITK.py", line 16, in swig_import_helper
    return importlib.import_module('_SimpleITK')
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: /home/tassilowald/Code/build/SimpleITK-build/Wrapping/Python/_SimpleITK.so: undefined symbol: PyClass_Type

To me this seems like the module was compiled(?) or installed(?) erronously for python 2 instead of my virtualenvs python 3.9 version, since PyClass_Type is python 2 specific. To test this i created a virtualenv with a python 2.7 interpreter and it indeed works there. However I don't want it in python 2, but python 3.

What do I have to change in the installation process for it to be usable in my python 3 virtualenv?

Also in the "${BUILD_DIR}/SimpleITK-build/Wrapping/Python/build" directory there is a version for lib.linux-x86_64-2.7/ and lib.linux-x86_64-3.9. I'm not sure if this is relevant but it seems like at least the setup.py call deliberately tries to install it for the correct interpreter. Surprisingly it also installs it without error or warnings for my python 3 environment (which I would expect to fail when the cmake process did only compile a python 2 version).

Here is the output of the python .Packaging/setup.py install call:


running bdist_egg
running egg_info
writing SimpleITK.egg-info/PKG-INFO
writing dependency_links to SimpleITK.egg-info/dependency_links.txt
writing top-level names to SimpleITK.egg-info/top_level.txt
reading manifest file 'SimpleITK.egg-info/SOURCES.txt'
writing manifest file 'SimpleITK.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build/lib.linux-x86_64-3.9
creating build/lib.linux-x86_64-3.9/SimpleITK
copying /home/tassilowald/Code/build/SimpleITK-build/Wrapping/Python/__init__.py -> build/lib.linux-x86_64-3.9/SimpleITK
copying /home/tassilowald/Code/build/SimpleITK-build/Wrapping/Python/SimpleITK.py -> build/lib.linux-x86_64-3.9/SimpleITK
running build_ext
copying /home/tassilowald/Code/build/SimpleITK-build/Wrapping/Python/_SimpleITK.so -> build/lib.linux-x86_64-3.9/SimpleITK/_SimpleITK.cpython-39-x86_64-linux-gnu.so
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/SimpleITK
copying build/lib.linux-x86_64-3.9/SimpleITK/__init__.py -> build/bdist.linux-x86_64/egg/SimpleITK
copying build/lib.linux-x86_64-3.9/SimpleITK/SimpleITK.py -> build/bdist.linux-x86_64/egg/SimpleITK
copying build/lib.linux-x86_64-3.9/SimpleITK/_SimpleITK.cpython-39-x86_64-linux-gnu.so -> build/bdist.linux-x86_64/egg/SimpleITK
byte-compiling build/bdist.linux-x86_64/egg/SimpleITK/__init__.py to __init__.cpython-39.pyc
byte-compiling build/bdist.linux-x86_64/egg/SimpleITK/SimpleITK.py to SimpleITK.cpython-39.pyc
creating stub loader for SimpleITK/_SimpleITK.cpython-39-x86_64-linux-gnu.so
byte-compiling build/bdist.linux-x86_64/egg/SimpleITK/_SimpleITK.py to _SimpleITK.cpython-39.pyc
installing package data to build/bdist.linux-x86_64/egg
running install_data
copying /home/tassilowald/Code/SimpleElastix/LICENSE -> build/bdist.linux-x86_64/egg/
copying /home/tassilowald/Code/SimpleElastix/NOTICE -> build/bdist.linux-x86_64/egg/
copying /home/tassilowald/Code/SimpleElastix/README.md -> build/bdist.linux-x86_64/egg/
copying /home/tassilowald/Code/build/SimpleITK-build/ITK-5.1-NOTICE -> build/bdist.linux-x86_64/egg/
copying /home/tassilowald/Code/build/SimpleITK-build/ITK-5.1-README.md -> build/bdist.linux-x86_64/egg/
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying SimpleITK.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying SimpleITK.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying SimpleITK.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying SimpleITK.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
zip_safe flag not set; analyzing archive contents...
SimpleITK.__pycache__.SimpleITK.cpython-39: module references __file__
SimpleITK.__pycache__._SimpleITK.cpython-39: module references __file__
creating 'dist/SimpleITK-2.0.0rc2.dev910+ga138e-py3.9-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing SimpleITK-2.0.0rc2.dev910+ga138e-py3.9-linux-x86_64.egg
creating /home/tassilowald/Code/virtualenvs/simple/lib/python3.9/site-packages/SimpleITK-2.0.0rc2.dev910+ga138e-py3.9-linux-x86_64.egg
Extracting SimpleITK-2.0.0rc2.dev910+ga138e-py3.9-linux-x86_64.egg to /home/tassilowald/Code/virtualenvs/simple/lib/python3.9/site-packages
Adding SimpleITK 2.0.0rc2.dev910+ga138e to easy-install.pth file

Installed /home/tassilowald/Code/virtualenvs/simple/lib/python3.9/site-packages/SimpleITK-2.0.0rc2.dev910+ga138e-py3.9-linux-x86_64.egg
Processing dependencies for SimpleITK==2.0.0rc2.dev910+ga138e
Finished processing dependencies for SimpleITK==2.0.0rc2.dev910+ga138e```
TaWald commented 3 years ago

I found a solution: As expected cmake used the python2.7 executable.

To specify which python should be used you can use this snippet (adapt the paths to your own python executable, LIB and Include dir):

cmake -DPYTHON_EXECUTABLE:FILEPATH=/usr/bin/python3.9 -DPYTHON_INCLUDE_DIR:PATH=/usr/include/python3.9 -DPYTHON_LIBRARY:FILEPATH=/usr/lib/python3.9/config-3.9-x86_64-linux-gnu/libpython3.9.so ../SimpleElastix/SuperBuild

Should you not find the /usr/include/python3.9 directory with Python.h inside, you have probably not installed the package python3.9-dev. Do so via sudo apt-get install python3.9-dev and you should be good to go. :+1: