Some built-in python modules are erased after call to clear classes

4 次查看(过去 30 天)
I am trying to use a Python function from Matlab and I would like to put a Matlab structure in argument of the function.
I have the following error:
Unable to resolve the name py.dict
I can create a numpy array in Matlab, or also import user defined module and it works. Python seems to be properly installed.
When I check Python environement I have the following:
Version: "3.7"
Executable: "...\Local\Programs\Python\Python37\python.EXE"
Library: "...\Local\Programs\Python\Python37\python37.dll"
Home: "...\Local\Programs\Python\Python37"
Status: Loaded
ExecutionMode: InProcess
I am using Matlab R2019b.
Thank you for your help!


Mikael Martin
Mikael Martin 2023-6-7
I have just found a workaround.
It seems to work. My user psecific Python modules are updated and it does not erase some built-in classes.
I have created an intermediate function "Update_PythonModule()" and inside this function I call reloadPy().
function Update_PythonModule()
clear classes
P = py.sys.path;
% Add user specific python module
% find python script in ModulePath
if count(P,ModulePath) == 0
function reloadPy()

更多回答(1 个)

Shubham 2023-6-5
编辑:Shubham 2023-6-6
Hi Mikael,
There is an update to the previous answer that I provided.
I am not encountering any errors when running the code snippet you have provided in MATLAB on my system.
The error message "Unable to resolve the name py.dict" suggests that Matlab is not able to find or import the dict function from Python correctly. This can be caused by a problem with the Python environment variables or path, or could be a compatibility issue with the version of Matlab and Python you are using.
To troubleshoot this issue, you may want to try the following:
  1. Double-check that the correct version of Python is installed and listed in the Matlab environment variables, and that the Python executable path is set correctly.
  2. Check the compatibility between the versions of Matlab and Python you are using. For example, not all Matlab versions may support the latest version of Python.
You can refer to this documentation to check the compatibility of python and MATLAB versions.
  2 个评论
Grace Kepler
Grace Kepler 2023-6-15
Hi Mikael,
The issue in this post was most likely caused by the behavior that you encountered with "clear classes", which left the py. package in an inconsistent state.
The behavior that you encountered with "clear classes" is not a bug and is expected, based on the way that you used the "clear classes" command.
The 'classes' section of the documentation, states that 'clear classes' issues a warning and does not clear a class of objects if any of those objects still exist after the workspace is cleared. This means, for instance, that objects can still exist in persistent variables of functions or figure windows.
In your case, you created a bool and complex objects in one function but used 'clear classes' in a different function, which means that the objects in the first function are not cleared before calling 'clear classes' in the second function. As a result, the 'clear classes' command is cut short, leaving the 'py' package in an inconsistent state.
To fix this, you need to clear the objects in the first function before calling 'clear classes' in the second function to ensure that the workflow is successful.
However, we realize that the affect of "clear classes" on the py. package may not be obvious from the documentation, and we will work to improve the documentation, so other users can avoid this confusion in the future.



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




Community Treasure Hunt

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

Start Hunting!

Translated by