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

21 ビュー (過去 30 日間)
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?

採用された回答

MathWorks Support Team
MathWorks Support Team 2023 年 11 月 29 日
編集済み: MathWorks Support Team 2023 年 11 月 29 日
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 .

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePython Package Integration についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by