Skip to content

Instantly share code, notes, and snippets.

@somada141
Created March 8, 2015 04:52
Show Gist options
  • Save somada141/828ad946a2f96ea7a31c to your computer and use it in GitHub Desktop.
Save somada141/828ad946a2f96ea7a31c to your computer and use it in GitHub Desktop.
Build SimpleITK against Anaconda Python on OSX #python #anaconda #conda #simpleitk #itk #build #source #cmake #osx

The following process shows how one would build SimpleITK (from source) to link against and work with the Anaconda Python environment on Mac OSX.

The majority of this process can be applied to other non-vanilla Python interpreters such as Enthought Canopy and Enthough Python Distribution (EPD). This process has also been tried on Windows 7 with Canopy.

Here's how one would go about building SimpleITK against their Anaconda Python:

  • Activate your preferred conda environment. I will assume its called py27 but be careful to amend appropriately and activate through source activate <environment_name>
  • Go to this link: http://www.itk.org/Wiki/SimpleITK/GettingStarted#Build_It_Yourself and take a look at the instructions for building SimpleITK first.
  • Make sure you have CMake installed (I used the 2.8.12 version while writing this but you may want to opt for the newest CMake version first).
  • Make sure you have the gcc and g++ compilers installed. I had XCode installed which comes with those. Test that in your terminal through 'gcc -v' and 'g++ -v'

Again, the above only applies to OSX. Linux systems would still use gcc/g++ while Windows systems should compile with Visual Studio.

  • In your terminal 'cd' to some directory where you want to download the code.
  • Clone the SimpleITK Git repo through git clone http://itk.org/SimpleITK.git. This obviously requires that you have git installed.
  • Once checkout is complete type cd SimpleITK so you can go into the newly created directory.
  • Switch to the release branch as you don't want to build whatever buggy version lives in the master branch by entering git checkout release.
  • Go up one directory through cd ..
  • Create a new directory called SimpleITK-build by entering mkdir SimpleITK-build. Now this directory should be alongside the SimpleITK directory containing the source code.
  • Go into the SimpleITK-build directory by entering cd SimpleITK-build.
  • Launch the CMake GUI tool using the SuperBuild in the source directory as such cmake-gui ../SimpleITK/SuperBuild.
  • Now you should get the CMake window. Make sure the Advanced checkbox is enabled.
  • Hit the Configure button and choose Native Unix Makefiles.
  • Now we have to configure the build to work with Anaconda Python:
    • Use the Search: field and enter WRAP. Out of the 8 entries or so disable everything except WRAP_PYTHON.
    • Hit Configure again to update the entries.
    • Search for PYTHON.
    • Now we have to set the appropriate paths to the Python interpreter, headers, and dylib. This is important so be careful. Check exactly where your conda environment resides. Mine was called py27 and the anaconda folder was located under the root so my environment directory was /anaconda/envs/py27/. Make sure to amend the paths I will use to match yours!
    • Set the PYTHON_EXECUTABLE to /anaconda/envs/py27/bin/python (again, amend as necessary).
    • Set PYTHON_INCLUDE_DIR to /anaconda/envs/py27/include/python2.7 (amend).
    • Set PYTHON_LIBRARY to /anaconda/envs/py27/lib/libpython2.7.dylib.
  • Now delete whatever you have written in the Search field to see all entries.
  • Make sure the BUILD_EXAMPLES, BUILD_SHARED_LIBS, and BUILD_TESTING checkboxed entries are all off.
  • Now all the rest should be fine and you should be fine. Hit Configure again and then hit Generate.
  • If you get any errors at this point then sth is off with your environment and I would suggest your reinstall/update your XCode. If it does make sure the Python paths you set are correct!
  • Close CMake and go back to the terminal.
  • Assuming you're still under the SimpleITK-build directory all you need to do is build the project. Enter make or (if you have multiple cores) make -j 4 (for quad-core) to run the build on multi-core. It'll be faster (duh).
  • Once the build completes you should be almost ready to go.
  • While still in the SimpleITK-build directory run the following to test the generated .so file: otool -L SimpleITK-build/Wrapping/_SimpleITK.so. Should CMake have screwed up as it tends to do on OSX it should tell you that its linking against the system python and not Anaconda.
  • Let's install SimpleITK anyway and then fix it. Being in the SimpleITK-build directory (next to the freshly checked out copy of SimpleITK in the SimpleITK dir) cd into the Wrapping dir as such cd SimpleITK-build/Wrapping.
  • Install the package into your conda environment (which we activate in the first step) through python PythonPackage/setup.py install.
  • Now if you did everything right you should be able to start an ipython session (ipython command in terminal) and try to import SimpleITK. You should get that horrible error again.
  • Now you have to apply a nifty little hack and hardcode the correct library. Exit the IPython session (Ctrl+D) and get back into the terminal session. We can now hack the correct library reference into the copy of the SimpleITK .so file that got installed into your conda environment. This is done with the following command:
sudo install_name_tool -change libpython2.7.dylib /anaconda/envs/py27/lib/libpython2.7.dylib /anaconda/envs/py27/lib/python2.7/site-packages/SimpleITK-0.8.0.post48-py2.7-macosx-10.5-x86_64.egg/SimpleITK/_SimpleITK.so

HOWEVER! Be careful mate :). Make sure that:

  • /anaconda/envs/py27/lib/libpython2.7.dylib is correct.
  • that the _SimpleITK.so file is indeed under /anaconda/envs/py27/lib/python2.7/site-packages/SimpleITK-0.8.0.post48-py2.7-macosx-10.5-x86_64.egg/SimpleITK/_SimpleITK.so.

Alternatively, amend the paths correctly.

Once you run the above command (successfully) you can try to start IPython again and 'import SimpleITK' and it should go through fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment