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 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 havegit
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 enteringgit checkout release
. - Go up one directory through
cd ..
- Create a new directory called
SimpleITK-build
by enteringmkdir SimpleITK-build
. Now this directory should be alongside theSimpleITK
directory containing the source code. - Go into the
SimpleITK-build
directory by enteringcd SimpleITK-build
. - Launch the
CMake GUI tool
using theSuperBuild
in the source directory as suchcmake-gui ../SimpleITK/SuperBuild
. - Now you should get the CMake window. Make sure the
Advanced
checkbox is enabled. - Hit the
Configure
button and chooseNative Unix Makefiles
. - Now we have to configure the build to work with Anaconda Python:
- Use the
Search:
field and enterWRAP
. Out of the8
entries or so disable everything exceptWRAP_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 theanaconda
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
.
- Use the
- Now delete whatever you have written in the
Search
field to see all entries. - Make sure the
BUILD_EXAMPLES
,BUILD_SHARED_LIBS
, andBUILD_TESTING
checkboxed entries are all off. - Now all the rest should be fine and you should be fine. Hit
Configure
again 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-build
directory all you need to do is build the project. Entermake
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 theSimpleITK
dir)cd
into theWrapping
dir as suchcd SimpleITK-build/Wrapping
. - Install the package into your
conda
environment (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
ipython
session (ipython
command 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
.so
file that got installed into yourconda
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.