# no libvtkRenderingTk with pip install vtk

**URL:** https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613
**Category:** Support
**Created:** [April 22, 2021, 2:16pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613 "2021-04-22T14:16:37Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![itan](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@itan](https://discourse.vtk.org/u/itan)
#### Post date: [April 22, 2021, 2:16pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/1 "2021-04-22T14:16:37Z")

</div>

Hi, I am on ubuntu 20.04 and trying to build a small GUI with Python 3.8.5, VTK 9.0.1 and Tkinter. Using VTK and Tkinter separately both work well, but I am having difficulties combining them.

I have tried it with a `vtkTkRenderWindowInteractor` and a `vtkTkRenderWidget` so far, but I obtain the error:

`_tkinter.TclError: couldn't load file "libvtkRenderingTk-9.0.so": libvtkRenderingTk-9.0.so: cannot open shared object file: No such file or directory`

I installed vtk via `pip install vtk` in my virtual environment. If I look inside the env/lib/python3.8/site-packages/vtkmodules there is a tk folder with the python files defining the classes I used. But there is indeed no libvtkRenderingTk.

Is it necessary to build vtk myself to achieve this? I guess with VTK\_WRAP\_PYTHON=ON and VTK\_USE\_TK=ON? I expected the pip installation to install all python modules and have all python functionality.

---

<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 22, 2021, 2:21pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/2 "2021-04-22T14:21:58Z")

</div>

The `pip` wheels only contain what doesn’t require external dependencies. I guess Tk is probably fine since Python usually ships with it. Not sure how Tk-less Python builds will handle the wheel then. If the auditwheel machinery doesn’t complain, we can enable that module for future wheel builds.

You can compile your own wheels by following [these steps](https://gitlab.kitware.com/vtk/vtk/blob/master/Documentation/dev/build.md#python-wheels).

---

<div class="post-metadata">

### Author: ![itan](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@itan](https://discourse.vtk.org/u/itan)
#### Post date: [April 22, 2021, 2:56pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/3 "2021-04-22T14:56:44Z")

</div>

Great, thank you for the quick reply! I was not aware of that. I will try those instructions.

---

<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 22, 2021, 3:09pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/4 "2021-04-22T15:09:53Z")

</div>

Yes, `VTK_WRAP_PYTHON` ,`VTK_USE_TK`, and `BUILD_SHARED_LIBS` are enough to build `libvtkRenderingTk-9.0.so`

But even when it’s built, it still must be installed in the correct location before it can be used. CMake puts it into “libs”, but it isn’t just a library. It’s a Tk extension module. If it is placed inside python’s `site-packages` directory, then the `vtkTkRenderWidget` should be able to find it.

It might be possible to put it in the pypi wheel. As Ben said, Python includes the Tk run-time libraries. The trick is how to build it, since Python distros do not include all the Tk headers and other pieces needed for the build. So it’s easy to build on linux (just install tk-dev beforehand), but it can be very challenging on Windows.

I’ve attached a simple cone example that you can use for testing:  
[vtkRenderWidgetConeExample.py](https://discourse.vtk.org/uploads/short-url/y5nYhK4F3DKiCmteSU0QKcAx56j.py) (1.3 KB)

Edit: since nothing links to or attempts to load libvtkRenderingTk.so until vtkTkRenderWidget is imported, there’s no harm in including it with Tk-less Python. It won’t interfere with anything else.

---

<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 22, 2021, 4:07pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/5 "2021-04-22T16:07:00Z")

</div>

> [@dgobbi](#):
>
> until vtkTkRenderWidget is imported

Note that `import vtk` imports all of VTK, so the “easy” import will hit this problem.

---

<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 22, 2021, 4:31pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/6 "2021-04-22T16:31:15Z")

</div>

It actually won’t, since “`import vtk`” does not import `vtkmodules.tk.vtkTkRenderWidget`.

The vtkRenderingTk lib isn’t a Python extension module and it isn’t loaded like one. It’s loaded into Tk by the machinery in `vtkmodules.tk.vtkLoadPythonTkWidgets`. For convenience, `vtkLoadPythonTkWidgets` looks for `vtkRenderingTk` in `sys.path`, but only because the code was written that way.

---

<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 22, 2021, 5:16pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/7 "2021-04-22T17:16:09Z")

</div>

Ah, I forgot that the Tk module is quite an oddball 🙂 .

---

<div class="post-metadata">

### Author: ![nicoco](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/7c8e57/32.png) [@nicoco](https://discourse.vtk.org/u/nicoco)
#### Post date: [February 2, 2022, 11:31am UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/8 "2022-02-02T11:31:17Z")

</div>

Hey all,

Thanks to this thread I managed to use VTK with tkinter and it’s great. However, I have to set `LD_LIBRARY_PATH=/path/to/my/env/lib/pythonX.X/site-packages/vtkmodules` before running my tkinter app. It is a minor inconvenience, but was wondering if there was a ‘cleaner’ way to set this up?

I tried this:

```auto
import os
import site
os.environ["LD_LIBRARY_PATH"] = os.path.join(site.getsitepackages()[0], "vtkmodules")

```

at the very top of my python entrypoint, but for some reason it does not work.

Any advice on how I could make this work, preferably in a cross-platform-friendly way?

---

<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: [February 2, 2022, 1:49pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/9 "2022-02-02T13:49:00Z")

</div>

Adding an RPATH of `$ORIGIN` would suffice. I think this is fixed in `master`, but isn’t in any release yet.

---

<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: [February 2, 2022, 1:56pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/10 "2022-02-02T13:56:31Z")

</div>

Hi Nicoco,

Try playing around with the `pathlist` in [`vtkLoadPythonTkWidgets.py`](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Wrapping/Python/vtkmodules/tk/vtkLoadPythonTkWidgets.py) to see if you can get it to load without setting `LD_LIBRARY_PATH`.

You can also try moving `libvtkRenderingTk*.so` from `vtkmodules` out to the same directory as `libvtkCommonCore*.so*` and the other VTK shared libraries.

The `libvtkRenderingTk*.so` is actually not a Python extension module, it’s a Tk extension module (which is why Python uses tkinter to load it). So it’s an odd beast.

You can also experiment with loading it by hand:

```python
import vtkmodules.all as vtk
import tkinter
root = tkinter.Tk()
root.tk.call('load', ...)

```

where the tcl “load” command is documented [here.](https://www.tcl.tk/man/tcl/TclCmd/load.html)

Out of curiosity, what Linux distro are you using?

David

---

<div class="post-metadata">

### Author: ![nicoco](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/7c8e57/32.png) [@nicoco](https://discourse.vtk.org/u/nicoco)
#### Post date: [February 2, 2022, 3:08pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/11 "2022-02-02T15:08:08Z")

</div>

Thanks for your answers!

I realized I did not include the error message in my first post 🤦

```auto
    self.vtk_render_window_interactor = vtkTkRenderWindowInteractor(
  File "/home/nicoco/.cache/pypoetry/virtualenvs/xxx-research-r1WBkZBg-py3.9/lib/python3.9/site-packages/vtkmodules/tk/vtkTkRenderWindowInteractor.py", line 56, in __init__
    vtkLoadPythonTkWidgets(master.tk)
  File "/home/nicoco/.cache/pypoetry/virtualenvs/xxx-research-r1WBkZBg-py3.9/lib/python3.9/site-packages/vtkmodules/tk/vtkLoadPythonTkWidgets.py", line 87, in vtkLoadPythonTkWidgets
    interp.call('load', filename, pkgname)
_tkinter.TclError: couldn't load file "libvtkRenderingTk-9.1.so": libvtkRenderingTk-9.1.so: cannot open shared object file: No such file or directory

```

> [@ben.boeckel](#):
>
> Adding an RPATH of `$ORIGIN` would suffice.

Well… I have no idea what this means 😃

> [@dgobbi](#):
>
> Try playing around with the `pathlist` in [`vtkLoadPythonTkWidgets.py`](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Wrapping/Python/vtkmodules/tk/vtkLoadPythonTkWidgets.py) to see if you can get it to load without setting `LD_LIBRARY_PATH`.

Looking at this file, I realized that `sys.path` is involved, so I tried…

```py
import os
import site
import sys
sys.path.append(os.path.join(site.getsitepackages()[0], "vtkmodules"))

```

…and it worked!

> [@dgobbi](#):
>
> You can also try moving `libvtkRenderingTk*.so` from `vtkmodules` out to the same directory as `libvtkCommonCore*.so*` and the other VTK shared libraries.

They are already in the same dir:

```auto
nicoco@licorne ~/s/i/i/research (master)> 
fdfind libvtkCommonCore /home/nicoco/.cache/pypoetry/virtualenvs/xxx-research-r1WBkZBg-py3.9
/home/nicoco/.cache/pypoetry/virtualenvs/xxx-research-r1WBkZBg-py3.9/lib/python3.9/site-packages/vtkmodules/libvtkCommonCore-9.1.so
/home/nicoco/.cache/pypoetry/virtualenvs/xxx-research-r1WBkZBg-py3.9/lib/python3.9/site-packages/vtkmodules/libvtkCommonCore.so
nicoco@licorne ~/s/i/i/research (master)> 
fdfind libvtkRenderingTk /home/nicoco/.cache/pypoetry/virtualenvs/xxx-research-r1WBkZBg-py3.9
/home/nicoco/.cache/pypoetry/virtualenvs/xxx-research-r1WBkZBg-py3.9/lib/python3.9/site-packages/vtkmodules/libvtkRenderingTk-9.1.so
/home/nicoco/.cache/pypoetry/virtualenvs/xxx-research-r1WBkZBg-py3.9/lib/python3.9/site-packages/vtkmodules/libvtkRenderingTk.so

```

> [@dgobbi](#):
>
> Out of curiosity, what Linux distro are you using?

Debian bullseye.

Not sure if this is related, but I did not use `make install` or anything requiring root privileges when building VTK, to try and keep track of what is actually necessary to get my small GUI working. I used `python3 setup.py bdist_wheel` after `make -j` and installed the wheel in my poetry virtualenv, through pip.

Anyway, thanks for your help!

---

<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: [February 2, 2022, 3:37pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/12 "2022-02-02T15:37:12Z")

</div>

> [@nicoco](#):
>
> `sys.path.append(os.path.join(site.getsitepackages()[0], "vtkmodules"))`

While that might work, it’s the wrong fix. But it gave me a clue about where the real fix needs to go, in `vtkLoadPythonTkWidgets.py` at line 40:

```diff
     # create an extensive list of paths to search
- pathlist = sys.path
+ pathlist = [os.path.join(p, 'vtkmodules') for p in sys.path]

```

Can you try adding this change to your `vtkLoadPythonTkWidgets.py` and check to see if it fixes the problem?

---

<div class="post-metadata">

### Author: ![nicoco](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/7c8e57/32.png) [@nicoco](https://discourse.vtk.org/u/nicoco)
#### Post date: [February 2, 2022, 3:47pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/13 "2022-02-02T15:47:07Z")

</div>

> [@dgobbi](#):
>
> it’s the wrong fix

Yes, it’s a dirty fix.

> [@dgobbi](#):
>
> Can you try adding this change to your `vtkLoadPythonTkWidgets.py` and check to see if it fixes the problem?

It does fix the problem! But patching stuff directly in installed packages feels even more dirty 🙂

Now tell me all of this will make it to the next official VTK wheel build so I don’t have to bother compiling anything and just use `vtk = "^9.1.1"` in my `pyproject.toml` 😉

---

<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: [February 2, 2022, 4:12pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/14 "2022-02-02T16:12:36Z")

</div>

> [@nicoco](#):
>
> Now tell me all of this will make it to the next official VTK wheel build so I don’t have to bother compiling anything and just use `vtk = "^9.1.1"` in my `pyproject.toml` 😉

I will patch `vtkLoadPythonTkWidgets.py` for the next release (VTK 9.2).

If you want the Tk widget in the official wheels, then consider submitting a VTK patch that adds it to the wheels ([see the cmake files here](https://gitlab.kitware.com/vtk/vtk/-/tree/master/.gitlab/ci)). Otherwise, it just ain’t gonna happen.

---

<div class="post-metadata">

### Author: ![nicoco](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/7c8e57/32.png) [@nicoco](https://discourse.vtk.org/u/nicoco)
#### Post date: [February 2, 2022, 4:18pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/15 "2022-02-02T16:18:40Z")

</div>

> [@dgobbi](#):
>
> Otherwise, it just ain’t gonna happen

OK I can try this, thanks for the advice!

---

<div class="post-metadata">

### Author: ![nicoco](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/7c8e57/32.png) [@nicoco](https://discourse.vtk.org/u/nicoco)
#### Post date: [February 17, 2022, 1:59pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/16 "2022-02-17T13:59:32Z")

</div>

Hey @dgobbi,

Besides the patch in `vtkLoadPythonTkWidgets.py`, I now have to manually symlink `libvtkRenderingTk-9.1.so` to `libvtkRenderingTk.so` in order to get the tk widget working.

I am unsure if I changed some CMake that results in this behaviour…

Anyway, I have a linux-only dirty fix:

```auto
from pathlib import Path
from warnings import warn

import vtk

major, minor, _ = vtk. __version__.split(".")

regular_file = Path(vtk. __file__ ).parent / f"libvtkRenderingTk.so"
if not regular_file.exists():
    warn("VTK does not seem to have TK support")
else:
    symlink = Path(vtk. __file__ ).parent / f"libvtkRenderingTk-{major}.{minor}.so"
    if not symlink.exists():
        warn(f"{symlink} does not exists")
        symlink.symlink_to(regular_file)

```

---

<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: [February 17, 2022, 3:10pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/17 "2022-02-17T15:10:57Z")

</div>

Do the other VTK libraries have a 9.1 suffix, for example, `libvtkCommonCore-9.1.so`?

If the cmake configuration changed in some way that removed the version suffix, then that might be the cause of the problem, because `vtkLoadPythonTkWidgets.py` requires the suffix.

---

<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: [February 17, 2022, 4:18pm UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/18 "2022-02-17T16:18:55Z")

</div>

> [@dgobbi](#):
>
> If the cmake configuration changed in some way that removed the version suffix, then that might be the cause of the problem, because `vtkLoadPythonTkWidgets.py` requires the suffix.

There’s the `VTK_VERSIONED_INSTALL` which indicates whether it has a suffix (and `VTK_CUSTOM_LIBRARY_SUFFIX` for the actual suffix of libraries; needs a `-` separator if it is non-empty).

---

<div class="post-metadata">

### Author: ![nicoco](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/7c8e57/32.png) [@nicoco](https://discourse.vtk.org/u/nicoco)
#### Post date: [March 1, 2022, 6:18am UTC](https://discourse.vtk.org/t/no-libvtkrenderingtk-with-pip-install-vtk/5613/19 "2022-03-01T06:18:07Z")

</div>

> [@dgobbi](#):
>
> `libvtkCommonCore-9.1.so`?

They don’t, but it doesn’t seem to be a problem. I might be responsible, since CMake is basically black magic to me. Next time I need to build my VTK wheel I’ll try to understand, for now my dirty fix works for me.

As usual, thanks for your answers!
