Some built-in python modules are erased after call to clear classes
조회 수: 14 (최근 30일)
이전 댓글 표시
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!
채택된 답변
추가 답변 (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:
- 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
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 Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!