Some built-in python modules are erased after call to clear classes
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mikael Martin
le 29 Mai 2023
Commenté : Grace Kepler
le 15 Juin 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!
Réponse acceptée
Plus de réponses (1)
Shubham
le 5 Juin 2023
Modifié(e) : Shubham
le 6 Juin 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 commentaires
Grace Kepler
le 15 Juin 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.
Voir également
Catégories
En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!