Error using feval -- Unrecognized function or variable 'py.dict' when trying to use pyrunfile or pyrun.

12 次查看(过去 30 天)
I’m getting “Error using feval Unrecognized function or variable 'py.dict” when trying to use pyrunfile or pyrun.
I’m running Matlab version: '24.2.0.2923080 (R2024b) Update 6' on a 16” 2023 Apple M2 Max with Sonoma 14.7.2 I installed python by downloading the dmg from python.org for version 3.12 and installing it. I can run python functions and modules using the “py.” prefix, but can’t run anything using pyun or pyrunfile. I’m not sure if I need to use those functions but a worried something might be wrong. Any suggestions are appreciated.
I looked through various posts about this error to verify that I have a compatible version of python with the “arm” version:
pyenv
ans =
PythonEnvironment with properties:
Version: "3.12"
Executable: "/usr/local/bin/python3"
Library: "/Library/Frameworks/Python.framework/Versions/3.12/lib/libpython3.12.dylib"
Home: "/Library/Frameworks/Python.framework/Versions/3.12"
Status: Loaded
ExecutionMode: OutOfProcess
ProcessID: "54235"
ProcessName: "MATLABPyHost"
And from the python command line:
>>> import platform, subprocess
>>> platform.processor()
'arm'
I can run the sample test program in a file called df1.py:
def myfunc():
return 'version 1'
By typing:
a= py.df1.myfunc
a =
Python str with no properties.
version 1
I can change the python code (to return 2) and reload it and get:
a= py.df1.myfunc
a =
Python str with no properties.
version 2
But if I try to run the file using pyrunfile, I get an error:
pyrunfile('df1.py')
Error using feval
Unrecognized function or variable 'py.dict'.
Same with pyrun:
res = py.list({"Name1","Name2","Name3"})
res =
Python list with values:
['Name1', 'Name2', 'Name3']
Use string, double or cell function to convert to a MATLAB array.
pyrun('res = list({"Name1","Name2","Name3"})')
Error using feval
Unrecognized function or variable 'py.dict'.
Any suggestions are appreciated.

采纳的回答

Hitesh
Hitesh 2025-5-12
Hi Darral Freund,
I tried executing the same set of commands that produced an error in your case, but I am not encountering any issues while running them on my system with the same specifications you mentioned (macOS, MATLAB R2024b, and Python 3.12). However, the error "Unrecognized function or variable 'py.dict'" suggests that MATLAB is internally trying to use "py.dict" but cannot find it. This is unusual, since you are able to use "py.list" and "py.df1.myfunc" without any problems.
However follow the below troubleshooting steps thta might help to resolve this issue:
Check Python version in MATLAB:
  • Check what version of Python MATLAB is really using
pyversion
pyenv
and in MATLAB. Ensure they match your expectations.
py.sys.version
py.sys.executable
Try a clean Restart:
  • Quit MATLAB.
  • Restart your Mac.
  • Start MATLAB again and try the minimal "py.dict" test.
Reinstall Python:
Try with a fresh installation of Python (sometimes more compatible with MATLAB on ARM):
brew install python@3.12
Then point MATLAB to this Python:
pyenv('Version','/opt/homebrew/bin/python3')
Update MATLAB
You're on R2024b Update 6, which is very recent, but double-check for a newer update.
For more information regarding "Call Python From MATLAB", kindly refer to the following MATLAB documentation:

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

标签

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by