Python Console: undefined symbol: Py_Main upon import

I’m facing an undefined symbol: Py_Main error upon entering command import vtk to Blender Python Console. This error does not occur when executing same command from Blender’s python3.9 executable, so I wonder what could be wrong.

Long story

Using off-the-shelf blender-2.93.5-linux-x64.tar.xz. I need vtk Python module in Blender, which I’ve installed to Blender’s Python environment by commands

cd blender-2.93.5-linux-x64/2.93/python/bin
./python3.9 -m ensurepip
./python3.9 -m pip install vtk==9.0.3

When I start up blender --factory-startup, go to Blender Python Console and issue the command, I get this error:

>>> import vtk
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
  File "/home/x/blender-2.93.5-linux-x64/2.93/python/lib/python3.9/site-packages/vtk.py", line 30, in <module>
    all_m = importlib.import_module('vtkmodules.all')
  File "/home/x/blender-2.93.5-linux-x64/2.93/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/x/blender-2.93.5-linux-x64/2.93/python/lib/python3.9/site-packages/vtkmodules/all.py", line 48, in <module>
    from .vtkRenderingMatplotlib import *
ImportError: /home/x/blender-2.93.5-linux-x64/2.93/python/lib/python3.9/site-packages/vtkmodules/libvtkPythonInterpreter-9.0.so: undefined symbol: Py_Main

However, if I run the same import command in python executable blender-2.93.5-linux-x64/2.93/python/bin/python3.9, the module loads without errors. Any idea what’s the cause?

Same error occurs with vtk==9.0.2 and blender-2.93.0-linux-x64.tar.xz. However, this used to work without problems in Blender 2.83 series with vtk==9.0.1. Something changed in Python or Blender?

@sybren if you have time at some point, please have a look. I got no ideas left what to try next. Thanks!

I don’t know the answer, but I suspect that it has to do with different versions of Python. Try using ./python3.9 -m pip install --no-binary --force-reinstall vtk==9.0.3

Thanks, but looks like --no-binary vtk does not work:

blender-2.93/2.93/python/bin$ ./python3.9 -m pip install --force-reinstall --no-binary vtk vtk==9.0.3
ERROR: Could not find a version that satisfies the requirement vtk==9.0.3 (from versions: none)
ERROR: No matching distribution found for vtk==9.0.3

It’s a mystery why import vtk works from Python interpreter but not from Blender. Maybe there is something incompatible, the package name installed is vtk-9.0.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

Updates:

  • Tried with latest versions. import vtk works OK in Blender in Windows 10 (Blender 2.93.6 and VTK 9.1.0) but not on Ubuntu 20.04.
  • Does not work in Ubuntu 21.10 (running in VirtualBox)
  • Does not work with Blender-3.0.0 on Ubuntu 20.04.

Have you found any solution yet? I am struggling with the same problem.

Hi,

A workaround is available, which enables import vtk not to fail immediately. The workaround is to comment out this line

#from .vtkRenderingMatplotlib import *

in file 2.93/python/lib/python3.9/site-packages/vtkmodules/all.py
Seems to work for Blender 3.0 as well. The exact reason for the import failure is still unknown, though.