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
py27but be careful to amend appropriately and activate throughsource 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 havegitinstalled. - Once checkout is complete type
cd SimpleITKso you can go into the newly created directory. - Switch to the
releasebranch as you don't want to build whatever buggy version lives in the master branch by enteringgit checkout release. - Go up one directory through
cd .. - Create a new directory called
SimpleITK-buildby enteringmkdir SimpleITK-build. Now this directory should be alongside theSimpleITKdirectory containing the source code. - Go into the
SimpleITK-builddirectory by enteringcd SimpleITK-build. - Launch the
CMake GUI toolusing theSuperBuildin the source directory as suchcmake-gui ../SimpleITK/SuperBuild. - Now you should get the CMake window. Make sure the
Advancedcheckbox is enabled. - Hit the
Configurebutton and chooseNative Unix Makefiles. - Now we have to configure the build to work with Anaconda Python:
- Use the
Search:field and enterWRAP. Out of the8entries or so disable everything exceptWRAP_PYTHON. - Hit
Configureagain 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
py27and theanacondafolder 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_EXECUTABLEto/anaconda/envs/py27/bin/python(again, amend as necessary). - Set
PYTHON_INCLUDE_DIRto/anaconda/envs/py27/include/python2.7(amend). - Set
PYTHON_LIBRARYto/anaconda/envs/py27/lib/libpython2.7.dylib.
- Use the
- Now delete whatever you have written in the
Searchfield to see all entries. - Make sure the
BUILD_EXAMPLES,BUILD_SHARED_LIBS, andBUILD_TESTINGcheckboxed entries are all off. - Now all the rest should be fine and you should be fine. Hit
Configureagain and then hitGenerate. - 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-builddirectory all you need to do is build the project. Entermakeor (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-builddirectory run the following to test the generated.sofile: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-builddirectory (next to the freshly checked out copy of SimpleITK in theSimpleITKdir)cdinto theWrappingdir as suchcd SimpleITK-build/Wrapping. - Install the package into your
condaenvironment (which we activate in the first step) throughpython PythonPackage/setup.py install. - Now if you did everything right you should be able to start an
ipythonsession (ipythoncommand in terminal) and try toimport 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
.sofile that got installed into yourcondaenvironment. 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.dylibis correct.- that the
_SimpleITK.sofile 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.