matlab/sim​ulink+tens​orflow problem

My MATLAB can connect to Python and execute Python statements, but it encounters an error when importing TensorFlow. How can I resolve this?
In the MATLAB command line, when I enter:
py.importlib.import_module('tensorflow')
an error occurs
“error to use pywrap_tensorflow><module> (line 88)
Python error ImportError: Traceback (most recent call last):
File "C:\Users\LENOVO\.conda\envs\matlab_test\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 73, in <module>
from tensorflow.python._pywrap_tensorflow_internal import *
ImportError: DLL load failed while importing _pywrap_tensorflow_internal: Dynamic link library (DLL) initialization routine failed.
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/errors for some common causes and solutions.
If you need help, create an issue at https://github.com/tensorflow/tensorflow/issues and include the entire stack trace above this error
message.
Error in __init__><module> (line 40)
Error in <frozen importlib>_call_with_frames_removed (line 228)
Error in <frozen importlib>exec_module (line 850)
Error in <frozen importlib>_load_unlocked (line 680)
Error in <frozen importlib>_find_and_load_unlocked (line 986)
Error in <frozen importlib>_find_and_load (line 1007)
Error in <frozen importlib>_gcd_import (line 1030)
Error in __init__>import_module (line 127)
My matlab version is R2024a, python== 3.9, tensorflow-cpu==2.19
When I import TensorFlow in the Python command line, there is no problem, but when I do so in MATLAB, the above error occurs.
I've tried a few solutions, but none of them worked:
  1. Install Microsoft Visual C++ Redistributable
  2. Switch tensorflow from GPU version to CPU version
  3. Use Anaconda to isolate dependencies for different projects.
please help me !!!!!

回答(1 个)

Umar
Umar 41 minutes 前
编辑:Umar 34 minutes 前
Hi @Sato, This one's actually more of a MATLAB-Python environment issue than a TensorFlow problem, even though the error makes it look like TF is the culprit. What's happening is that MATLAB launches Python without fully inheriting your conda environment's variables, so TensorFlow's native C++ DLLs can't be located at load time — even though everything looks fine when you run Python directly. First thing to check — run pyenv in your MATLAB command window and see what path it's pointing to. If it's not your matlab_test conda environment, that's likely where things are going wrong. You can point it to the right place with: pyenv(Version='C:\Users\LENOVO\.conda\envs\matlab_test\python.exe') Restart MATLAB after doing this, since pyenv changes don't take effect in the same session. Weblink: https://www.mathworks.com/help/matlab/ref/pyenv.html If pyenv is already pointing to the right env, the next thing to try is switching to OutOfProcess execution mode. By default MATLAB runs Python in the same memory space as itself, and TensorFlow's runtime is quite sensitive to that setup. Switching to OutOfProcess tells MATLAB to spin up a separate Python host process instead, which avoids the DLL conflict entirely: pyenv(ExecutionMode='OutOfProcess') Try your import again after that — this tends to be the fix for most people hitting this exact error. Weblink: https://www.mathworks.com/help/matlab/matlab_external/install-supported-python-implementation.html Also worth thinking about how you're opening MATLAB. If you're launching it from a desktop shortcut or the Start Menu, it won't inherit your conda environment's PATH even if pyenv is configured correctly. Instead, try opening Anaconda Prompt, activating your environment with conda activate matlab_test, and then launching MATLAB from there. That way MATLAB starts with all the right environment variables already in place. Weblink: https://www.mathworks.com/matlabcentral/answers/1450474-how-to-add-the-path-to-a-conda-python-environment One more quick thing to check — run getenv('PYTHONHOME') in MATLAB. If that variable is set and pointing somewhere other than your conda env, it can silently override pyenv and cause exactly this kind of DLL failure. Clearing it with setenv('PYTHONHOME', '') is worth a shot if it's pointing somewhere unexpected. And if none of the above fully resolves it, it's worth knowing that TF 2.19 quietly dropped Python 3.9 support in some Windows builds. As a last resort, try downgrading to tensorflow-cpu 2.15 or 2.16 — both are well tested with Python 3.9 on Windows and might just clear things up. Hope that helps — let me know how it goes!

类别

帮助中心File Exchange 中查找有关 Call Python from MATLAB 的更多信息

产品

版本

R2024a

提问:

2026-3-19,3:26

编辑:

about 12 hours 前

Community Treasure Hunt

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

Start Hunting!

Translated by