cmake: VTK_PYTHON_VERSION ignored, python 3.5 not found (VTK 8.2, ubuntu 16.04)

I’m trying to build VTK 8.2 with bindings for python 3.5, so I set VTK_PYTHON_VERSION=3 (or 3.5), but cmake can’t find it, it finds just 2.7 instead:

Could NOT find PythonInterp: Found unsuitable version “2.7.12”, but
required is at least “3” (found /usr/bin/python2)

I googled, found a suggestion to force PYTHON_EXECUTABLE=/usr/bin/python3 (which DOES exist), but then I get an error about libraries mismatch:

CMake Warning at CMake/vtkPythonWrapping.cmake:14 (message):
Python library 2.7 mismatches python 3.5.

I tried to force PYTHON_LIBRARIES, but I still get the mismatch warning. Here’s the line:

cmake -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_LIBRARIES=/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu/libpython3.5.so -DVTK_PYTHON_VERSION=3 -S VTK-8.2.0 -B VTK-8.2.0.build/

If I ignore the warnings and go ahead and build, it builds only bindings for 2.7 (in VTK-8.2.0.build/lib/python2.7, nothing for 3.5)

Any idea what’s going on, and how can I fix this? Alternatively, is there a wheel somewhere for VTK 8.2 and python 3.5 for ubuntu 16.04? The pip3 version of VTK is 8.1.x, which I had to hack to make it work with PySide2, but 8.2 should have that fixed that’s why I’m trying to install it.

This is an output variable of FindPythonLibs. Set PYTHON_LIBRARY instead.

thanks @ben.boeckel, it works!
At first I was still getting the mismatched libraries message, but after clearing the cmake cache it went away.

Another note (for some this is obvious, but I’m sure many noobs like me with find this helpful):
I had to also set PYTHONPATH to make ‘import vtk’ work: ‘sudo make install’ puts the package in ‘/usr/local/lib/python3.5/site-packages’, which is not in the search path for imports. Under ubuntu (and other distributions derived from debian), we find instead ‘/usr/local/lib/python3.5/dist-packages’, which is the destination for packages installed by pip(3). I’ve done this by adding this line to /etc/environment:

PYTHONPATH="/usr/local/lib/python3.5/site-packages"

(I didn’t come up with this solution myself, I found it online, but unfortunately I haven’t saved the links)

That install path can be changed via the build’s cache entries.

That’s even better. Do you remember the name of the variable?

Ah, it’s not in the cache by default. VTK_PYTHON_SITE_PACKAGES_SUFFIX

I would be cautious with manual installation of VTK python modules to *buntu’s dist-packages though. That is reserved for things installed through Ubuntu packages. I would install VTK as a whole to a non-system-wide prefix and point PYTHONPATH / LD_LIBRARY_PATH there.