# Integrating ITK Python extension build into setup.py (distutils)

**URL:** https://discourse.itk.org/t/integrating-itk-python-extension-build-into-setup-py-distutils/341
**Category:** Engineering
**Created:** [October 20, 2017, 11:08am UTC](https://discourse.itk.org/t/integrating-itk-python-extension-build-into-setup-py-distutils/341 "2017-10-20T11:08:18Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![sakoho](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@sakoho](https://discourse.itk.org/u/sakoho)
#### Post date: [October 20, 2017, 11:08am UTC](https://discourse.itk.org/t/integrating-itk-python-extension-build-into-setup-py-distutils/341/1 "2017-10-20T11:08:18Z")

</div>

Hi,

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 [setup.py](http://setup.py) 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.

Best,

Sami

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [October 20, 2017, 12:35pm UTC](https://discourse.itk.org/t/integrating-itk-python-extension-build-into-setup-py-distutils/341/2 "2017-10-20T12:35:25Z")

</div>

Hi Sami,

The best approach is to put your ITK classes in an ITK module, and [a Python package can be easily generated](https://blog.kitware.com/python-packages-for-itk-modules/). This uses [scikit-build](https://blog.kitware.com/python-packages-for-itk-modules/) to drive the CMake build of the Python C extension.

HTH,  
Matt

CC: @jcfr

---

<div class="post-metadata">

### Author: ![ncullen93](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/8baadc/32.png) [@ncullen93](https://discourse.itk.org/u/ncullen93)
#### Post date: [October 20, 2017, 2:13pm UTC](https://discourse.itk.org/t/integrating-itk-python-extension-build-into-setup-py-distutils/341/3 "2017-10-20T14:13:05Z")

</div>

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](https://github.com/ANTsX/ANTsPy)

You can also check out the ANTsPy [ITK wrap guide](https://github.com/ANTsX/ANTsPy/blob/master/tutorials/UsingITK.md), 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(['./build_library.sh'])` to the setup file, but that wont wrap it for you automatically
