Integrating ITK Python extension build into (distutils)


I’m looking into a project that is mostly written in Python, but also contains a number of external modules for ITK and VTK written in C++ that need to be built and Python wrapped upon installation.

I am wondering whether it would be possible to somehow use the setuptools/distutils to trigger the CMAKE build of the extension modules, so that when one writes python install, the extensions would be automatically compiled and wrapped? I use the build_ext() command regularly with simple C extensions, but I couldn’t figure out how to adapt it to work with CMakelists.txt files.

I’m interested in this, because both ITK and VTK are now available through Anaconda and that sort of direct build would make working with external/extension modules easier in Python.



Hi Sami,

The best approach is to put your ITK classes in an ITK module, and a Python package can be easily generated. This uses scikit-build to drive the CMake build of the Python C extension.


CC: @jcfr

1 Like

Hi Sami!

I’m not totally clear on what you’re trying to do, but Matt’s suggestion is definitely the easiest way. In case you can’t do that though… you should check out the ANTsPy library which does exactly what you’re asking – builds ITK+VTK libraries and wraps custom ITK/C++ code in python … all from the setup file . The relevant C++ and CMakeLists.txt is in antspy/ants/lib directory.

Link to repository

You can also check out the ANTsPy ITK wrap guide, which may give you some insights.

Pybind11 is a good way to wrap C++ code, fyi. And in case your question is as simple as “how do I build a C++ library from a python setup file?” then the answer is "write a bash script and add a line like subprocess.check_call(['./']) to the setup file, but that wont wrap it for you automatically

1 Like