Why do I receive a 'GLIBCXX not found' or 'unable to resolve the name' error when using a Python module with MATLAB?

115 views (last 30 days)
When I try to use scipy (version 1.10.0) with Python 3.9 and MATLAB R2022b on Ubuntu 20.04, I get an 'unable to resolve the name' error
>> pts = py.numpy.random.default_rng().random([int32(30), int32(3)]);
>> kdtree = py.scipy.spatial.KDTree(pts)
Unable to resolve the name 'py.scipy.spatial.KDTree'.
When I try to import scipy.spatial in MATLAB, I get a different error: "../sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.29' not found"
>> py.importlib.import_module('tensorflow')
>> spatial = py.importlib.import_module('scipy.spatial')
Error using _kdtree><module>
Python Error: ImportError:
<matlabroot>/bin/glnxa64/../../sys/os/glnxa64/libstdc++.so.6:
version `GLIBCXX_3.4.29' not found (required by
<path to virtual environment>/lib/python3.9/site-packages/scipy/spatial/_ckdtree.cpython-39-x86_64-linux-gnu.so)
...
How can I use my module with Python Interface?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 29 Nov 2023
Edited: MathWorks Support Team on 29 Nov 2023
This error message indicates a difference between the MATLAB and Python versions of libstdc++. You can fix this issue using the following steps.
1) Locate the libstdc++ library used by Python. You can do this by using "ldd".
ldd <path to spatial module library>/_ckdtree.cpython-310-x86_64-linux-gnu.so
2) Set LD_PRELOAD to the libstdc++ library path found from step 1. For example, with a bash shell execute this command.
$ export LD_PRELOAD=<path to libstdc++library>/libstdc++.so.6:$LD_PRELOAD
You can use the echo command to confirm that LD_PRELOAD is set up correctly.
$ echo $LD_PRELOAD
3) Launch MATLAB from the same terminal used for setting LD_PRELOAD in step 2.
4) For releases R023a, and earlier, set up the MATLAB Python environment to use "InProcess" execution mode.
>> pyenv('ExecutionMode','InProcess')
For releases R2023b, and later, this workaround should work for both "InProcess" and "OutOfProcess" execution mode.
5) Test
>> pts = py.numpy.random.default_rng().random([int32(30), int32(3)]);
>> kdtree = py.scipy.spatial.KDTree(pts)
kdtree =
Python KDTree with properties:
>> spatial = py.importlib.import_module('scipy.spatial')
spatial =
Python module with properties:
voronoi_plot_2d: [1×1 py.function]
cKDTree: [1×1 py.type]
For other troubleshooting tips for 'unable to resolve name' errors, see this MathWorks documentation page .

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!