# Local libraries with Python wrapping

**URL:** <https://discourse.vtk.org/t/local-libraries-with-python-wrapping/6808>\
**Category:** Development\
**Created:** [October 12, 2021, 11:08am UTC](https://discourse.vtk.org/t/local-libraries-with-python-wrapping/6808 "2021-10-12T11:08:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![djduke](https://discourse.vtk.org/user_avatar/discourse.vtk.org/djduke/32/2174_2.png) [@djduke](https://discourse.vtk.org/u/djduke)\
**Post date:** [October 12, 2021, 11:08am UTC](https://discourse.vtk.org/t/local-libraries-with-python-wrapping/6808/1 "2021-10-12T11:08:12Z")

</div>

I’m after advice on the current recommended route for creating a local library for which the  
classes can be wrapped [Python]  
Previously releases provided an  
Examples/Build/vtkLocal directory that could be used as a template for local libraries.  
This seems to have been deprecated at least its not in the git archive.  
The replacement Examples/Build/vtkMy provides scaffolding for a local library  
but des not seem to support wrapping, at least not via Python. The classes in the  
example library are used via C++. Am I missing something, or is there an  
example somewhere of a local library with wrapped classes that I could follow?

I’ve nothing against going back to 1st principles and starting again from CMake  
but there used to be a good solution and if the problem is already solved  
its unnecessarily wasteful.

thanks,  
David

---

<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:** [October 12, 2021, 12:12pm UTC](https://discourse.vtk.org/t/local-libraries-with-python-wrapping/6808/2 "2021-10-12T12:12:05Z")

</div>

The new example is [here](https://gitlab.kitware.com/vtk/vtk/-/tree/master/Examples/Modules/Wrapping). In VTK 9, the wrapping of local libraries is more-or-less automated (more so than before), but the local libraries must be structured as a VTK module.

In the example, the important pieces are:

1. A subdirectory (the module, any name can be used) with a `vtk.module` file
2. A `CMakeLists.txt` file with `vtk_module_wrap_python` to wrap the module

---

<div class="post-metadata">

**Author:** ![djduke](https://discourse.vtk.org/user_avatar/discourse.vtk.org/djduke/32/2174_2.png) [@djduke](https://discourse.vtk.org/u/djduke)\
**Post date:** [October 18, 2021, 9:58am UTC](https://discourse.vtk.org/t/local-libraries-with-python-wrapping/6808/3 "2021-10-18T09:58:51Z")

</div>

Thanks David, Exactly what I was looking for. However In trying to built the local example as included as a test  
I’ve run into issues:

1.CMake Error at CMakeLists.txt:11 (include):  
include could not find requested file:

```
 /vtkExternalModuleMacros.cmake

```

If I look in /usr/local/libcmake/vtk that macro is indeed not present  
I find it present when using 6.1, 6.3, 7.1, 9.0

If I cp the file to the 9.1 dir and rererun cmake I get

include could not find requested file:

/vtkExternalModuleMacros.cmake  
made me wonder if the problem is in cmake concatenating  
${VTK\_CMAKE\_DIR} with the filename . However  
the core vtk build is fine. I’ve tried multiple versions  
of Cmake.

2.from line 8 of CMakeLists  
The `VTK_USE_FILE` is no longer used starting with 8.90

I’m using CMake 3.8.0 to satisfy the version constraint 3.8…3.12  
I’m having the issues with both the VTK-9.0.1 tarball and the git head.

Any suggestions?  
thanks,  
David

---

<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:** [October 18, 2021, 1:10pm UTC](https://discourse.vtk.org/t/local-libraries-with-python-wrapping/6808/4 "2021-10-18T13:10:18Z")

</div>

Hi David, the example that I pointed to is [Examples/Modules/Wrapping](https://gitlab.kitware.com/vtk/vtk/-/tree/master/Examples/Modules/Wrapping)

It’s very possible that the old Examples/Build/vtkLocal is not working anymore.

* * *

Edit: The vtkLocal example was removed from ‘master’ in March, and it’s not in the current release branch either. So it’s a dead end, it’s been superseded by the Examples/Modules examples.

---

<div class="post-metadata">

**Author:** ![djduke](https://discourse.vtk.org/user_avatar/discourse.vtk.org/djduke/32/2174_2.png) [@djduke](https://discourse.vtk.org/u/djduke)\
**Post date:** [October 20, 2021, 6:08pm UTC](https://discourse.vtk.org/t/local-libraries-with-python-wrapping/6808/5 "2021-10-20T18:08:24Z")

</div>

Thanks David for theclarifications, got that working now.

---

<div class="post-metadata">

**Author:** ![djduke](https://discourse.vtk.org/user_avatar/discourse.vtk.org/djduke/32/2174_2.png) [@djduke](https://discourse.vtk.org/u/djduke)\
**Post date:** [November 1, 2021, 12:29pm UTC](https://discourse.vtk.org/t/local-libraries-with-python-wrapping/6808/6 "2021-11-01T12:29:09Z")

</div>

Thanks. I could get the example module built and working from boththe interactive vtkpython prompt and with the example python script  
inthe Testing directory.  
However if I try to add my own class to the module I get

AttributeError: module ‘wrapping.vtkWrappable’ has no attribute ‘vtkGrid’

where vtkGrid is the name of the class I want to add & instantiate

I suspect Ive missed a step, I

put the .h, .cc files in the module directory

ensured the header file was including the module header, and put

the appropriate export in the class declaration

UpdateCMakeLists to add the new class.

rerun CMake, built and installed.

The example class vtkWrappable is working fine, my class is not

have I missed anything, or am I using wrong steps to

instantiate froma module?

If anyone has time to look I attach a tarball

of my directory. Th new class is vtkGrid

Testing/import\_grid.py is the python script I wrote to test,

based on the import\_wrappable script

thanks, David

[meta.tgz](https://discourse.vtk.org/uploads/short-url/6ZQtBlQiPTVHhWipL98lMMGQGPz.tgz) (38.9 KB)

---

<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:** [November 1, 2021, 1:49pm UTC](https://discourse.vtk.org/t/local-libraries-with-python-wrapping/6808/7 "2021-11-01T13:49:53Z")

</div>

You were close, there were just a couple errors holding you back.

Syntax error in CMakeLists.txt:

```diff
 set(classes
- vtkGrid,vtkWrappable)
+ vtkGrid
+ vtkWrappable
+)

```

Missing dependency in vtk.module:

```diff
 NAME
   Wrapping::Wrappable
 LIBRARY_NAME
   vtkWrappable
 DEPENDS
   VTK::CommonCore
+ VTK::CommonExecutionModel

```

The VTK 9 wrapping puts the module (in this case, ‘vtkWrappable’) into a package (in this case, ‘wrapping’) so imports are like this:

```diff
-from wrapping import vtkWrappable
-from wrapping import vtkGrid
+from wrapping.vtkWrappable import vtkWrappable
+from wrapping.vtkWrappable import vtkGrid
 
-wrap = vtkWrappable.vtkWrappable()
+wrap = vtkWrappable()
 print( 'test=', wrap.GetString())
 print('instantiate grid')
-grid = vtkWrappable.vtkGrid()
-grid.PrintSelf()
+grid = vtkGrid()
+print(grid)

```

Note that the `PrintSelf()` isn’t called directly from Python (or from C++ either). In C++ use ‘`grid->Print()`’, and in Python use ‘`print(grid)`’.

---

<div class="post-metadata">

**Author:** ![djduke](https://discourse.vtk.org/user_avatar/discourse.vtk.org/djduke/32/2174_2.png) [@djduke](https://discourse.vtk.org/u/djduke)\
**Post date:** [November 20, 2021, 11:15am UTC](https://discourse.vtk.org/t/local-libraries-with-python-wrapping/6808/8 "2021-11-20T11:15:41Z")

</div>

Thanks David. Not clear this is strictly the same issue however I didn’t think it  
worth creating a new thread

I’m running into a different problem now. Trying to build the  
Examples/Modules/Wrappable from VTK9.1  
Attempting to ccmake I get  
but it set VTK\_FOUND to FALSE so package “VTK” is considered to be NOT  
FOUND. Reason given by package:

Could not find the VTK package with the following required components:  
Python.

CMake Warning at CMakeLists.txt:10 (find\_package):  
Found package configuration file:  
Skipping example: Could not find the VTK package with the following required components: Python.  
VTK is built and installed and python scripts are working . Paths appear to be okay.

Any thoughts on what might trigger this error might help me track down the issue.

thanks  
David

---

<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:** [November 20, 2021, 2:58pm UTC](https://discourse.vtk.org/t/local-libraries-with-python-wrapping/6808/9 "2021-11-20T14:58:08Z")

</div>

You haven’t given me much information to work with. Is your installed VTK one that you built yourself? What are the details of its configuration, e.g. BUILD\_SHARED\_LIBS, which version of Python, is there anything special about your environment?

I just ran a check on my own system (ubuntu 20.04, VTK 9.1, using ubuntu’s python 3.8) and Examples/Modules/Wrappable built just fine. My VTK was built with BUILD\_SHARED\_LIBS=ON and VTK\_WRAP\_PYTHON=ON and I used “make install” to do the installation.

When I configured the Wrappable example, I set VTK\_DIR=\<install-prefix\>/lib/cmake/vtk-9.1 (the VTK\_DIR should be set to the directory where vtk-config.cmake is located).

---

<div class="post-metadata">

**Author:** ![djduke](https://discourse.vtk.org/user_avatar/discourse.vtk.org/djduke/32/2174_2.png) [@djduke](https://discourse.vtk.org/u/djduke)\
**Post date:** [November 22, 2021, 11:10am UTC](https://discourse.vtk.org/t/local-libraries-with-python-wrapping/6808/10 "2021-11-22T11:10:36Z")

</div>

Hi David,  
Sorry - I tried to do a clean build and think I found the problem.  
When I try to run ccmake in the dir for the example Wrappable example I get

Parse error. Expected “(”, got newline with text "

I’ve looked at the CMake file with vi and od but can’t see

why its generating this error, would you check the attached please.

Although given the above they may be moot To answer your Qs from your reply post.

I’m using my own build of VTK9.1 I’m on OSX 11.6.1

I attach the CMake Cache from my vtk build

[CMakeLists.txt](https://discourse.vtk.org/uploads/short-url/ct9ibD4vOwCn8SOU7V2iVsi7stv.txt) (1.83 KB)

[CMakeCache.txt](https://discourse.vtk.org/uploads/short-url/2EmC0KqyRQoGmvl4M6prZvlCPp0.txt) (161 KB)

---

<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:** [November 22, 2021, 1:13pm UTC](https://discourse.vtk.org/t/local-libraries-with-python-wrapping/6808/11 "2021-11-22T13:13:48Z")

</div>

> [@djduke](#):
>
> Parse error. Expected “(”, got newline with text "

CMake usually gives a line number when it reports a syntax error, like the following:

```plaintext
CMake Error at CMakeLists.txt:27:
  Parse error. Expected "(", got newline with text "

```

As for the error, it’s not in your main CMakeLists.txt, so maybe it’s in a CMakeLists.txt in a subdirectory?

That particular error means you have a stray word somewhere in the CMakeLists.txt that is neither followed by nor within parentheses. Valid cmake syntax:

```plaintext
AAAAA(BBBB CCCC ....)

```

Invalid syntax that will cause that parse error:

```plaintext
AAAAA

```

---

<div class="post-metadata">

**Author:** ![djduke](https://discourse.vtk.org/user_avatar/discourse.vtk.org/djduke/32/2174_2.png) [@djduke](https://discourse.vtk.org/u/djduke)\
**Post date:** [November 29, 2021, 11:41am UTC](https://discourse.vtk.org/t/local-libraries-with-python-wrapping/6808/12 "2021-11-29T11:41:02Z")

</div>

Apols for the delay in replying I tried diging deeper. I never isolated the location of the parse error but doing a build with a fresh copy cleared the error.  
however I’m now stuck with

> > > from wrapping import vtkWrappable

> > > wrap = vtkWrappable()

wrap = vtkWrappable()

Traceback (most recent call last):

File “”, line 1, in

TypeError: ‘module’ object is not callable

I looked in /usr/local/lib/python3.10/site-packages:

README.txt pip-20.2.3.dist-info vtk.py vtkmy

pip vtk vtkmodules wrapping

wrappig contains vtkWrappable.so

my PYTHONPATH is set to

/usr/local/lib/python3.10/site-packages/:/usr/local/lib/python3.10/site-packages/wrapping/:/usr/local/lib/python3.10/site-packages/vtkmodules/

If it helps I attach a tarball of the directory that I’m building the Wrappable module - should not be

an issue as its a direct copy of /Examples/Modules/Wrapping

I also attach the CMake Cache from my VTK9.1 build

thanks

David

[CMakeCache.txt](https://discourse.vtk.org/uploads/short-url/2EmC0KqyRQoGmvl4M6prZvlCPp0.txt) (161 KB)

[meta.tar](https://discourse.vtk.org/uploads/short-url/1YKuOTQL9UUmyDJsdX3ii7c0FD7.tar) (1.02 MB)

---

<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:** [November 29, 2021, 1:37pm UTC](https://discourse.vtk.org/t/local-libraries-with-python-wrapping/6808/13 "2021-11-29T13:37:04Z")

</div>

> [@djduke](#):
>
> TypeError: ‘module’ object is not callable

Try this sample code, which I posted above (as a diff), on Nov 1st:

```python
from wrapping.vtkWrappable import vtkWrappable
from wrapping.vtkWrappable import vtkGrid

wrap = vtkWrappable()
print( 'test=', wrap.GetString())
print('instantiate grid')
grid = vtkGrid()
print(grid)

```

To break this down:

- `wrapping` is the package (the `PYTHON_PACKAGE` as set in `CMakeLists.txt`)
- `wrapping.vtkWrappable` is the module (as set in `vtk.module` and elsewhere)
- `wrapping.vtkWrappable.vtkWrappable` is the class (as declared in `vtkWrappable.h`)

So, the following gives a class named ‘vtkWrappable’:

```python
from wrapping.vtkWrappable import vtkWrappable
wrap = vtkWrappable()

```

While this gives a module named `vtkWrappable`, of which the class is a member:

```python
from wrapping import vtkWrappable
wrap = vtkWrappable.vtkWrappable()

```

The example really should not have used the same name for the module and the class, since that’s what appears to be causing the confusion.
