# Python IDE autocompletion for upcoming VTK 9.1

**URL:** https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708
**Category:** Development
**Created:** [September 27, 2021, 9:27pm UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708 "2021-09-27T21:27:38Z")
**Posts on this page:** 20
**Page:** 1

<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: [September 27, 2021, 9:27pm UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/1 "2021-09-27T21:27:38Z")

</div>

Hi all, just today I merged a patch into VTK-master to improve the vtk-python method descriptors and docstrings. This should help autocompletion and hinting for ipython and jupyter. Anyone who wants to try it out, please do so, because VTK 9.1 is coming soon and I won’t have time to do additional testing myself.

if you have an IDE that uses .pyi files, there is a [script](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Utilities/Maintenance/vtk_generate_pyi.py) that you can run to generate them. Note that this script will only work with the current master branch, it will not work with yesterday’s master or with VTK 9.0.3.

@amaclean since you’re a PyCharm user, is there a chance that you can try this script to see if it makes any difference to the user experience? When run with no arguments, the script will simply deposit the .pyi files next to the .pyd files.

---

<div class="post-metadata">

### Author: ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)
#### Post date: [September 28, 2021, 5:44am UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/2 "2021-09-28T05:44:06Z")

</div>

Used Linux and it seems to work nicely. I ran it with no parameters. One suggestion, it might be good to mention that if the environment is not set up properly call it like this:

```auto
vtkpython ./vtk_generate_pyi.py

```

As regards PyCharm, it doesn seem to make any difference. PyCharm seems to do this when it loads the modules. However PyCharm is aware of the files because when I run `vtk_generate_pyi.py` it reindexes and also when these files are deleted.

---

<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: [September 28, 2021, 12:29pm UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/3 "2021-09-28T12:29:25Z")

</div>

Thanks for giving it a spin, Andrew. If the .pyi files cause PyCharm to reindex, then at least I know that it’s pulling information from them. And some of that information (e.g. @overload hints) is only available if the .pyi files are present.

@phcerdan you mentioned issues with vtk-python autocompletion in an earlier thread, If you can try the current master (or vtk-9.1-rc1 when it comes out in a few days), any feedback you can give would be greatly appreciated.

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/phcerdan/32/54_2.png) [@phcerdan](https://discourse.vtk.org/u/phcerdan)
#### Post date: [September 28, 2021, 7:33pm UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/4 "2021-09-28T19:33:14Z")

</div>

Thanks for the work @dgobbi!

Fast check: I am using pyright (language server) in vim trough the plugin coc.vim.  
I installed vtk in a python virtual environment from a wheel generated from the build tree of current master. And then I used the script to generate the stub pyi files.

I am able to get the info from .pyi stubs files in the editor. However I have to specify  
the vtkmodules path.

If I import the whole vtk package `import vtk` I can only access inner python methods

![image](https://discourse.vtk.org/uploads/default/original/2X/d/d78d9c6ab95b0449df8f44940ac9290d0c5301a3.png)

However if I import using the vtkmodules path it works flawlessly.

![image](https://discourse.vtk.org/uploads/default/original/2X/6/6a4d6fb8e1b6b420bca638a5ecd5a0f4539287b9.png)

---

<div class="post-metadata">

### Author: ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)
#### Post date: [September 28, 2021, 10:02pm UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/5 "2021-09-28T22:02:41Z")

</div>

It works really well in windows and PyCharm.

It even resolves this one (only happens in Windows):

- from `vtkmodules.vtkInteractionWidgets import vtkCameraOrientationWidget`  
`Cannot find reference 'vtkCameraOrientationWidget' in ' __init__.py'`

This is the cleanest code inspection for VTK in PyCharm that I have seen!

Next question:  
Will we have to run this every time we build VTK?

---

<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: [September 28, 2021, 11:57pm UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/6 "2021-09-28T23:57:48Z")

</div>

> [@amaclean](#):
>
> It works really well in windows and PyCharm.

Thanks, Andrew. One of the features I’m hoping people will find useful is the type hints, particularly for overloaded methods:

![overloads](https://discourse.vtk.org/uploads/default/original/2X/0/0a5a1d394537249d7ee82f3798a716585ec8aec5.png)  
This is the feature that I’m most interested in people testing on the various IDEs and Python consoles.

I strongly suspect that the ‘vtkCameraOrientationWidget’ error is due to leftovers on your system from an old build or something like that. The fact that it mentions an ` __init__.py` makes me think your system might have a `vtkInteractionWidgets` folder somewhere that it doesn’t belong.

I do not have any immediate plans to add the .pyi files to the VTK build, but wouldn’t object if someone else did so. I’ve found that ipython, jupyter, and the default Python console actually do fairly well just with the updated docstrings in the wrappers, and for them the .pyi files don’t seem to add much benefit (though I would be interested to hear from others on the subject).

---

<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: [September 29, 2021, 4:04am UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/7 "2021-09-29T04:04:05Z")

</div>

@phcerdan thanks for giving it a spin. Were you able to get any hinting for method argument types?

The script only generates vtkmodules .pyi files, it would need to be modified to generate a vtk.pyi (and the file would be very large). It’s something I could do, but I’m not convinced that it would be a good idea.

---

<div class="post-metadata">

### Author: ![estan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/estan/32/460_2.png) [@estan](https://discourse.vtk.org/u/estan)
#### Post date: [September 29, 2021, 7:07am UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/8 "2021-09-29T07:07:39Z")

</div>

Thanks a lot @dgobbi! This is awesome already. If at some point this could be available directly after pip installing vtk it would be great. /PyCharm user

---

<div class="post-metadata">

### Author: ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)
#### Post date: [September 29, 2021, 7:33am UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/9 "2021-09-29T07:33:08Z")

</div>

For those of you using `import vtk` you can easily start using the vtkmodules files. I have a script that looks through your code and provides you with a list of the necessary `from ... import ...` statements.

Please see [VTKImportsForPython](https://kitware.github.io/vtk-examples/site/Python/Utilities/VTKImportsForPython/), and ignore the comment about sorting. It needs testing as I am not sure that I capture all of the VTK classes since pattern matching is used.

Also there is a script [VTKModulesForCxx](https://kitware.github.io/vtk-examples/site/Python/Utilities/VTKModulesForCxx/) that will generate a `find_package(VTK COMPONENTS ...)` command for CMake, useful for Cxx development.

Both these scripts have some minor changes coming through (tomorrow?).

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/phcerdan/32/54_2.png) [@phcerdan](https://discourse.vtk.org/u/phcerdan)
#### Post date: [September 29, 2021, 8:45am UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/10 "2021-09-29T08:45:29Z")

</div>

> [@dgobbi](#):
>
> @phcerdan thanks for giving it a spin. Were you able to get any hinting for method argument types?

Yep!

 ![image](https://discourse.vtk.org/uploads/default/original/2X/9/9b3f5e1a740ca98a1f90523a267095020a55aaf0.png)

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/phcerdan/32/54_2.png) [@phcerdan](https://discourse.vtk.org/u/phcerdan)
#### Post date: [September 29, 2021, 9:19am UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/11 "2021-09-29T09:19:51Z")

</div>

> [@dgobbi](#):
>
> I do not have any immediate plans to add the .pyi files to the VTK build, but wouldn’t object if someone else did so. I’ve found that ipython, jupyter, and the default Python console actually do fairly well just with the updated docstrings in the wrappers, and for them the .pyi files don’t seem to add much benefit (though I would be interested to hear from others on the subject).

> [@estan](#):
>
> Thanks a lot @dgobbi! This is awesome already. If at some point this could be available directly after pip installing vtk it would be great. /PyCharm user

I would prefer to add the stubs directly to the vtk package. But as an alternative, another optional package `vtk-stubs`, `vtk-pyi` or similar name, can be created. The only inconvenient is that vtk and vtk-stubs might get out of sync with each other when updating vtk but not vtk-stubs.

The [setup.py.in](https://github.com/Kitware/VTK/blob/0b2ace1af5fbfffa40e2905291bcab6b94bc07ed/CMake/setup.py.in#L33-L40) file seems the place to trigger the stubs script after the build? I am not familiar with vtk python packaging.

---

<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: [September 29, 2021, 11:30am UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/12 "2021-09-29T11:30:38Z")

</div>

> [@phcerdan](#):
>
> The [setup.py.in](https://github.com/Kitware/VTK/blob/0b2ace1af5fbfffa40e2905291bcab6b94bc07ed/CMake/setup.py.in#L33-L40) file seems the place to trigger the stubs script after the build?

My inclination would be to trigger the script with cmake during the build, rather than with setup.py after the build. Putting the .pyi files in the build tree would allow us to add a test for them (e.g. the test could load them into Python to check the syntax, and could check for unresolved types, etc). But I don’t think we’re quite there 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: [September 29, 2021, 9:03pm UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/13 "2021-09-29T21:03:17Z")

</div>

Also, for people who are using “import vtk”, you might have better luck with this instead:

```nohighlight
import vtkmodules.all as vtk

```

That should be sufficient for PyCharm to take the “vtk” hints from “vtkmodules”. I don’t know whether it will work for other IDEs, however, since every IDE does hints differently.

---

<div class="post-metadata">

### Author: ![amaclean](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amaclean/32/224_2.png) [@amaclean](https://discourse.vtk.org/u/amaclean)
#### Post date: [September 30, 2021, 4:18am UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/14 "2021-09-30T04:18:56Z")

</div>

Thanks @dgobbi, it works really well in PyCharm, as soon as you type `vtk.` you get a dropdown list of all the classes etc. and the modules they live in! It’s really fast too. Love it, thanks for the tip!

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [September 30, 2021, 1:21pm UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/15 "2021-09-30T13:21:38Z")

</div>

I’ve been crazy busy and haven’t followed what’s going on with Python, sorry about that.

This morning I did a clean master pull / VTK build to test a new filter. I found that the way I’ve been testing/debugging Python test scripts for the last many years no longer works. For example:

~/Develop/Bisect/debug/bin/vtkpython TestExtractPoints.py

Gives me a bunch of import errors. As a member of the uninformed, unwashed masses, let me ask: what do I need to do to get this working again?

---

<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: [September 30, 2021, 1:49pm UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/16 "2021-09-30T13:49:47Z")

</div>

Hi Will, as long as your version of Python is somewhere between 3.6 and 3.9, running vtkpython as you have indicated should still work just fine. That’s how I usually run my test scripts, too. Most of the vtk-python tests haven’t changed in 5 years or more, so backwards compatibility should be pretty good.

What was the text of the error?

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [September 30, 2021, 2:12pm UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/17 "2021-09-30T14:12:03Z")

</div>

My python version is 3.6 on Ubuntu.

Load errors are as follows:

Traceback (most recent call last):  
File “TestExtractPoints.py”, line 2, in   
import vtk  
File “/home/will/Develop/Bisect/release/lib/python3.6/site-packages/vtk.py”, line 31, in   
all\_m = importlib.import\_module(‘vtkmodules.all’)  
File “/usr/local/lib/python3.6/importlib/ **init**.py”, line 126, in import\_module  
return \_bootstrap.\_gcd\_import(name[level:], package, level)  
File “/home/will/Develop/Bisect/release/lib/python3.6/site-packages/vtkmodules/all.py”, line 7, in   
from .vtkCommonCore import \*  
ImportError: /home/will/Develop/Bisect/release/lib/python3.6/site-packages/vtkmodules/[vtkCommonCore.cpython-36m-x86\_64-linux-gnu.so](http://vtkCommonCore.cpython-36m-x86_64-linux-gnu.so): undefined symbol: PyExc\_IndexError

---

<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: [September 30, 2021, 2:27pm UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/18 "2021-09-30T14:27:41Z")

</div>

> [@will.schroeder](#):
>
> undefined symbol: PyExc\_IndexError

This error means that libpython (libpython3.6.so) is not being linked, or perhaps there is a symbol visibility problem.  
@ben.boeckel do you have ideas about what might cause this?

---

<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: [September 30, 2021, 2:51pm UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/19 "2021-09-30T14:51:07Z")

</div>

I suspect the platform is not working with `VTK_PYTHON_OPTIONAL_LINK` in some way. Changing that option might help?

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [September 30, 2021, 3:14pm UTC](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708/20 "2021-09-30T15:14:33Z")

</div>

I unchecked that option (`VTK_PYTHON_OPTIONAL_LINK`) and rebuilt. VTK then failed to build:

[1/122] Linking CXX shared library lib/libvtkWrappingPythonCore3.6-9.0.so.9.0.0  
FAILED: lib/libvtkWrappingPythonCore3.6-9.0.so.9.0.0  
: && /usr/bin/c++ -fPIC -O3 -DNDEBUG -Wl,-lc -shared -Wl,-soname,libvtkWrappingPythonCore3.6-9.0.so.1 -o lib/libvtkWrappingPythonCore3.6-9.0.so.9.0.0 Wrapping/PythonCore/CMakeFiles/WrappingPythonCore.dir/PyVTKEnum.cxx.o Wrapping/PythonCore/CMakeFiles/WrappingPythonCore.dir/PyVTKExtras.cxx.o Wrapping/PythonCore/CMakeFiles/WrappingPythonCore.dir/PyVTKNamespace.cxx.o Wrapping/PythonCore/CMakeFiles/WrappingPythonCore.dir/PyVTKObject.cxx.o Wrapping/PythonCore/CMakeFiles/WrappingPythonCore.dir/PyVTKSpecialObject.cxx.o Wrapping/PythonCore/CMakeFiles/WrappingPythonCore.dir/PyVTKTemplate.cxx.o Wrapping/PythonCore/CMakeFiles/WrappingPythonCore.dir/vtkPythonArgs.cxx.o Wrapping/PythonCore/CMakeFiles/WrappingPythonCore.dir/vtkPythonCommand.cxx.o Wrapping/PythonCore/CMakeFiles/WrappingPythonCore.dir/vtkPythonOverload.cxx.o Wrapping/PythonCore/CMakeFiles/WrappingPythonCore.dir/vtkPythonUtil.cxx.o Wrapping/PythonCore/CMakeFiles/WrappingPythonCore.dir/vtkSmartPyObject.cxx.o Wrapping/PythonCore/CMakeFiles/WrappingPythonCore.dir/PyVTKMethodDescriptor.cxx.o Wrapping/PythonCore/CMakeFiles/WrappingPythonCore.dir/PyVTKReference.cxx.o -Wl,-rpath,/home/will/Develop/Bisect/release/lib: lib/libvtkCommonCore-9.0.so.9.0.0 lib/libvtksys-9.0.so.9.0.0 -ldl /usr/lib/x86\_64-linux-gnu/libtbb.so.2 /usr/local/lib/libpython3.6m.a -lpthread -ldl -lutil -Wl,-rpath-link,/home/will/Develop/Bisect/release/lib && :  
/usr/bin/ld: /usr/local/lib/libpython3.6m.a(pystate.o): warning: relocation against `_PyGILState_check_enabled' in read-only section `.text’  
/usr/bin/ld: /usr/local/lib/libpython3.6m.a(compile.o): relocation R\_X86\_64\_PC32 against symbol `PyExc\_SyntaxError’ can not be used when making a shared object; recompile with -fPIC  
/usr/bin/ld: final link failed: bad value  
collect2: error: ld returned 1 exit status  
ninja: build stopped: subcommand failed.

[Next page](https://discourse.vtk.org/t/python-ide-autocompletion-for-upcoming-vtk-9-1/6708.md?page=2)
