Garrett
Garrett

Reputation: 4414

Building SimpleITK with CMake for Python 3

I'd like to build SimpleITK from source and install it for Python 3.4 (on Ubuntu 14.04).

I followed SimpleITK's build instructions, i.e.

git clone http://itk.org/SimpleITK.git
mkdir SimpleITK-build
cd SimpleITK-build
cmake ../SimpleITK/SuperBuild

and that worked fine. I was able to install SimpleITK for Python 2.7 using sudo python setup.py install. I then tried to install it for Python 3.4, using sudo python3.4 setup.py install, and although it seemed to install correctly, when I tried to import SimpleITK in Python 3, I got:

...    
    File "/usr/lib/python3.4/imp.py", line 243, in load_module
        return load_dynamic(name, filename, file)
ImportError: dynamic module does not define init function (PyInit__SimpleITK)

In the build instructions above, it says:

Verify and/or correct the advanced cmake variables to the language specific executable, libraries and include directories. For example if you have multiple Python installations ensure that all related Python variable refer to the same versions.

I didn't do that, and I suspect that's why it will only work for Python 2.7. How do I correct the "advanced cmake variables"?

Upvotes: 3

Views: 1677

Answers (2)

Garrett
Garrett

Reputation: 4414

Here are all the steps I used on Linux (building of Yiuin's answer and the official docs).

Get SimpleITK source code

cd ~
git clone --recursive http://itk.org/SimpleITK.git

Set latest stable version

cd ~/SimpleITK
git tag

look for the latest stable version (for me it was "v0.8.0") and check it out by doing

git checkout v0.8.0

and don't worry about the warning about the 'detached HEAD'.

Build SimpleITK

cd ~
mkdir SimpleITK-build
cd SimpleITK-build

and here I used the CMake GUI (on Ubuntu, get it with sudo apt-get install cmake-qt-gui),

cmake-gui ../SimpleITK/SuperBuild/

This will open up a GUI where you can edit the cmake variables from. Click the button Configure and hit Finish on the dialog that pop, then enable Advanced. Change all the relevant Python variables:

enter image description here

(I'm not sure why some of the paths had 'm' at the end, but you can read more about it here)

If you want to turn off Java, or any other language, scroll down and disable WRAP_JAVA. Hit Configure again (not sure if that's necessary) and then hit Generate. Close the CMake GUI, go back to your terminal and enter

make

which takes at least an hour.

Now add this line to your .bashrc file

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/SimpleITK-build/lib

Install on Python

I used checkinstall to manage the installation cleanly,

cd ~
sudo checkinstall --pkgname python3-simpleitk  python3.4 \
~/SimpleITK-build/SimpleITK-build/Wrapping/PythonPackage/setup.py install

Upvotes: 6

Micah
Micah

Reputation: 452

I would first checkout the latest tagged version, its hard to know whats going on in the repo at any given time if you aren't on a tagged branch. use git tag to see which are available, then do a:

git checkout v0.8.0

To optimize for the correct cersion, you should probably do ccmake instead of cmake. Then do configure (c) twice and then generate (g). You can probably disable java, tcl and R if you aren't going to use them. Check that your python libraries and includes match python 3.4 (PYTHON_INCLUDE_DIR, PYTHON_LIBRARY, PYTHON_EXECUTABLE, use 't' to see the advanced options).

Upvotes: 1

Related Questions