# python bindings of vtk based tools

**URL:** https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003
**Category:** Support
**Created:** [April 9, 2020, 3:04pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003 "2020-04-09T15:04:35Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![kayarre](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kayarre/32/808_2.png) [@kayarre](https://discourse.vtk.org/u/kayarre)
#### Post date: [April 9, 2020, 3:04pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003/1 "2020-04-09T15:04:36Z")

</div>

In reading through this discussion on [Remote modules vs external modules](https://discourse.vtk.org/t/remote-modules-vs-external-modules/2003)

I want to create python bindings for a c++ project that heavily depends on VTK, and possibly make a paraview plugin with it. What would be the best way to approach this given the current state of development and roadmap.

I think it would be best to make a separate library, i.e.  
`import vtkdocoolsthings`  
but It would seem a waste not to be able to use VTK tooling to generate python wrappers?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [April 9, 2020, 5:01pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003/2 "2020-04-09T17:01:33Z")

</div>

This is a very important topic, as nowadays Python wrapping is a must (if a library is not Python-wrapped then it essentially does not exist) and all the developer groups working with VTK are facing this issue if they want to upgrade to VTK9.

There is one working example, @dgobbi’s [https://github.com/dgobbi/vtk-dicom/](https://github.com/dgobbi/vtk-dicom/) library, so one option is to copy-paste CMake scripts from there.

We are looking into generalizing this mechanism by creating a set of CMake functions that can Python-wrap 3D Slicer and its 150+ extensions with minimal changes compared to VTK-8.2. These macros will be first maintained in a separate repository (maybe later can be maintained as part of VTK) and the plan is to make it usable to wrap any VTK-based projects. @jcfr may be able to give more details on the current status.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [April 9, 2020, 5:06pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003/3 "2020-04-09T17:06:43Z")

</div>

There is an example in the repository now: [https://gitlab.kitware.com/vtk/vtk/-/tree/master/Examples%2FModules%2FWrapping](https://gitlab.kitware.com/vtk/vtk/-/tree/master/Examples%2FModules%2FWrapping)

We can expand on this more if needed, but it does work (modulo some `PATH` settings we need on Windows, but that’s a dashboard issue, not a code issue as such).

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [April 9, 2020, 5:44pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003/4 "2020-04-09T17:44:45Z")

</div>

I gotta say that my cmake scripts for vtk-dicom are not a great example for how to do this. They’re terribly convoluted, since they support half a dozen different configurations. For that very same reason, though, they expose things at a very low level.

For the curious, the important bits for externally wrapping vtk-dicom for VTK 9 are [**here**](https://github.com/dgobbi/vtk-dicom/blob/master/CMakeLists.txt#L175), and the module file itself is [**here**](https://github.com/dgobbi/vtk-dicom/blob/master/CMakeLists.txt#L175).

The vtk docs describe the cmake interface for python wrapping:  
[https://vtk.org/doc/nightly/html/group\_\_module-wrapping-python.html](https://vtk.org/doc/nightly/html/group__module-wrapping-python.html)

---

<div class="post-metadata">

### Author: ![devmessias](https://discourse.vtk.org/user_avatar/discourse.vtk.org/devmessias/32/3364_2.png) [@devmessias](https://discourse.vtk.org/u/devmessias)
#### Post date: [May 8, 2021, 6:44pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003/5 "2021-05-08T18:44:20Z")

</div>

Hi @ben.boeckel . I can’t figure out how to build the example from  
[https://gitlab.kitware.com/vtk/vtk/-/tree/master/Examples%2FModules%2FWrapping](https://gitlab.kitware.com/vtk/vtk/-/tree/master/Examples%2FModules%2FWrapping)  
Can you help me?

I tried the following steps:

1 - build vtk

```bash
cmake -DBUILD_SHARED_LIBS=ON -DBUILD_TESTING=ON \
  -DCMAKE_BUILD_TYPE=Release -DVTK_WRAP_PYTHON=ON -DVTK_PYTHON_VERSION=3 \
  ../vtk .

```

2 - Inside Examples/Modules/Wrapping

```bash
mkdir build 
cd build
cmake -DVTK_DIR=../../../../../vtkBuild/ . 

```

```bash
$ cd module
$ make install                                                                                      
[33%] Generating the wrap hierarchy for Wrapping::Wrappable
/bin/sh: 1: /home/devmessias/projects/fury/vtkBuild/bin/vtkWrapHierarchy-9.0: not found
make[2]: *** [module/CMakeFiles/vtkWrappable-hierarchy.dir/build.make:66: lib/vtk/hierarchy/Wrapping/vtkWrappable-hierarchy.txt] Error 127
make[1]: *** [CMakeFiles/Makefile2:1027: module/CMakeFiles/vtkWrappable-hierarchy.dir/all] Error 2
make: *** [Makefile:141: all] Error 2

```

Thanks

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [May 8, 2021, 7:07pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003/6 "2021-05-08T19:07:17Z")

</div>

The error is that one of the executables that VTK builds for wrapping is missing:

> [@](#):
>
> `/bin/sh: 1: /home/devmessias/projects/fury/vtkBuild/bin/vtkWrapHierarchy-9.0: not found`

The most likely explanation is that the VTK build did not finish. Are you sure that the build of VTK was successful?

---

<div class="post-metadata">

### Author: ![devmessias](https://discourse.vtk.org/user_avatar/discourse.vtk.org/devmessias/32/3364_2.png) [@devmessias](https://discourse.vtk.org/u/devmessias)
#### Post date: [May 8, 2021, 7:11pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003/7 "2021-05-08T19:11:35Z")

</div>

Hi! Yes. Everything worked fine.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [May 8, 2021, 7:14pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003/8 "2021-05-08T19:14:24Z")

</div>

Is there an executable called `vtkWrapHierarchy-9.0` in `vtkBuild/bin`?

What happens if you do `make WrapHierarchy` in your `vtkBuild` directory?

---

<div class="post-metadata">

### Author: ![devmessias](https://discourse.vtk.org/user_avatar/discourse.vtk.org/devmessias/32/3364_2.png) [@devmessias](https://discourse.vtk.org/u/devmessias)
#### Post date: [May 8, 2021, 7:18pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003/9 "2021-05-08T19:18:06Z")

</div>

Is there an executable called `vtkWrapHierarchy-9.0` in `vtkBuild/bin`  
no  
What happens if you do `make WrapHierarchy` in your `vtkBuild` directory?  
Ok, this created a new file

```bash
$ls bin
bin/vtkWrapHierarchy-9.0*

```

However, now I got a new error

```bash
make[2]: *** No rule to make target '/home/devmessias/projects/fury//vtkBuild/lib/libvtkCommonCore-9.0.so.9.0.1', needed by 'lib/libvtkWrappable.so'. Stop

```

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [May 8, 2021, 7:18pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003/10 "2021-05-08T19:18:56Z")

</div>

It looks like you didn’t build VTK. Just run “make” (or “make -j 4”) in your vtkBuild directory.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [May 8, 2021, 8:37pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003/11 "2021-05-08T20:37:35Z")

</div>

Note that we are working on providing examples (and later templates) that shows how to build VTK9-based packages that can be used by C++ applications that embed Python (3D Slicer, Paraview, etc.) and also distributed on PyPI. See more information about this effort [here](https://discourse.vtk.org/t/rfc-toward-supporting-distribution-of-vtk-based-modules-on-pypi/4873).

---

<div class="post-metadata">

### Author: ![devmessias](https://discourse.vtk.org/user_avatar/discourse.vtk.org/devmessias/32/3364_2.png) [@devmessias](https://discourse.vtk.org/u/devmessias)
#### Post date: [May 8, 2021, 8:44pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003/12 "2021-05-08T20:44:41Z")

</div>

Ah! OK! Thanks! It looks like it will work now.  
I have a doubt. I want to install this wrapper inside an anaconda environment.  
However, when I do make install inside Examples/Modules/Wrapping

```auto
-- Install configuration: ""
-- Installing: /usr/local/lib/python3.8/site-packages/wrapping/vtkWrappable.so
CMake Error at cmake_install.cmake:47 (file):
  file INSTALL cannot copy file
  "/home/.../Examples/Modules/Wrapping/lib/python3.8/site-packages/wrapping/vtkWrappable.so"
  to "/usr/local/lib/python3.8/site-packages/wrapping/vtkWrappable.so".

```

However, the python paths are set correctly  
 ![image](https://discourse.vtk.org/uploads/default/original/2X/0/086330b589b8bdc2163bb3b6430b09ec6a5d5111.png)

---

<div class="post-metadata">

### Author: ![devmessias](https://discourse.vtk.org/user_avatar/discourse.vtk.org/devmessias/32/3364_2.png) [@devmessias](https://discourse.vtk.org/u/devmessias)
#### Post date: [May 8, 2021, 8:48pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003/13 "2021-05-08T20:48:52Z")

</div>

CMAKE\_INSTALL\_PREFIX=/home/devmessias/anaconda3 ok, this worked!  
However, running the example gives a exception

```bash
$ python3 ../module/Testing/import_wrappable.py (base) 
Traceback (most recent call last):
  File "../module/Testing/import_wrappable.py", line 1, in <module>
    from wrapping import vtkWrappable
ImportError: libvtkWrappingPythonCore-9.0.so.1: cannot open shared object file: No such file or directory

```

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [May 8, 2021, 9:27pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003/14 "2021-05-08T21:27:43Z")

</div>

Some general instructions:

1. Find the directory for `libvtkWrappingPythonCore-9.0.so.1`.
2. Find the directory that your `vtkWrappable.so` is in.
3. Use a symbolic link to put all libs from (1) into (2):

```auto
ln -fs /path1/lib*.so.1 /path2/

```

If that doesn’t work, you can try adding the directory for (1) to your LD\_LIBRARY\_PATH.

And there are other solutions, like using `chrpath` to tell vtkWrappable.so where to look for libraries.

---

<div class="post-metadata">

### Author: ![devmessias](https://discourse.vtk.org/user_avatar/discourse.vtk.org/devmessias/32/3364_2.png) [@devmessias](https://discourse.vtk.org/u/devmessias)
#### Post date: [May 8, 2021, 10:01pm UTC](https://discourse.vtk.org/t/python-bindings-of-vtk-based-tools/3003/15 "2021-05-08T22:01:41Z")

</div>

Thanks for saving my Saturday @dgobbi. Everything working now. I’m trying to create a custom renderpass in python with thisc++ wrapper
