Some built-in python modules are erased after call to clear classes
14 views (last 30 days)
Show older comments
Mikael Martin
on 29 May 2023
Commented: Grace Kepler
on 15 Jun 2023
Hello,
I am trying to use a Python function from Matlab and I would like to put a Matlab structure in argument of the function.
MyStruct=struct('Test',1);
py.dict(MyStruct)
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!
Accepted Answer
More Answers (1)
Shubham
on 5 Jun 2023
Edited: Shubham
on 6 Jun 2023
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:
- 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.
- 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.
2 Comments
Grace Kepler
on 15 Jun 2023
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.
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!