ITK/Python Wrapping
Python Wrapping
This section describes how to wrap ITK classes for being used from Python.
Options
There are two options for wrapping ITK with Python -- only select one in your CMake configuration!
WrapITK
WrapITK is the newest and best option. There is a version distributed with ITK, but it can be stale. The latest version can be found here.
CSWIG
CSWIG is an older but still supported option.
Microsoft Windows CSWIG
In order to wrap ITK for Python under Microsoft Windows you must start by gathering the following materials - at least this is the configuration with which I succeeded
- Python 2.4
- ITK CVS version posterior to July 10 2004 - I used 3.2.0
- CableSwig- I used 2.2
It is convenient to use CSwig from CVS and to check it out inside the source tree of ITK. However this is not necessary as the path to CableSwig can be set in the CMake configuration for ITK.
CMake configuration:
- Compiler: I tried MS Visual Studio Express which failed. However, I think this because VS Express does not include the MS Windows SDK. I believe that installing that (free download from MS) and adding it to the "VC directories" paths correctly in VS Express will make it work. I have MS VC 2003 .NET as well, and both CableSwig and ITK compiled without errors.
- Shared Libs ON
- ITK_CSWIG_PYTHON ON
- Set the python paths (Advanced Values), don't worry about PYTHON_DEBUG_LIBRARY
- TK_INTERNAL_PATH is the path to tkWinInt.h - this file is found in the source distro of Tcl/Tk.
- ITK_EXPLICIT_INSTANTATION must be off.
Open ALL_BUILD in MSVC, select the configuration you want (Release, RelWithDebugInfo etc) and build for a long time.
Compiling with NumArray:
It seems the CMake configuration for this combination needs some manual fine-tuning: If you see an error that import_libnumarray() is needed in itkPyBuffer.txx and cannot be found it is because this macro is defined in libnumarray.h (part of numarray) which is nowhere included. I added #include <libnumarray.h> in itkPyBuffer.h and the path to it in the MSVC directories (Tools->Options) and it compiled fine.
Installation:
Build the INSTALL target. This will install the binaries and the Python wrapper modules. If you receive a CMake error like "C:/packages/InsightToolkit-3.2.0/Wrapping/CSwig/cmake_install.cmake:31: FILE INSTALL cannot find file "C:/packages/InsightToolkit-3.2.0/Wrapping/CSwig/Python/InsightToolkit.py" to install." that's because the compilation configuration subdir (e.g. Release) is not being included, so CMake can’t find the file. Edit the paths in the .cmake file, and (re-)installation moves along fine.
The next same issue is pythonfiles_install.cmake - the FILE(GLOB command on the first line also needs to read bin/Release/*.py ( or replace "Release" with your configuration).
This is still true as of CVS version 6/17/07.
Once everything is installed correctly, go to the Lib\site-packages of your Python installation and create a text file named "InsightToolkit.pth". Add four lines like these -
C:\\Program Files\\ITK\\bin
C:\\Program Files\\ITK\\lib\\InsightToolkit
C:\\Program Files\\ITK\\lib\\InsightToolkit\\python
C:\\Program Files\\ITK\\lib\\InsightToolkit\\python\\Release
(or replace C:\Program Files with the root of your ITK installation. Then add C:\Program Files\ITK\bin to the %PATH% global variable (and the path to python.exe)
To test, start a cmd shell, type "python" and at the >>> prompt, "from InsightToolkit import *". You should see no errors.
Linux