How to Debug Python Script (using breakpoints) that is called from Matlab on MAC OS
15 ビュー (過去 30 日間)
古いコメントを表示
I am using MATLAB_R2023b.
I have wrote a Python script which is called from Matlab files. I want to debug the Python script. I have tried logging information using print statements in Python but that is not the preferred way to debug as I have to do a lot of work. I wanted to know if there are any work arounds like using visual studio code or other IDE for the same.
I am using MACOS, now that Visual Studio is retiring for MACOS how should one go about debugging it?
0 件のコメント
回答 (2 件)
Amish
2024 年 1 月 22 日
Hi Sammed,
I understand that you are tryying to find a way to debug python code from MATLAB. Since Visual Studio is retiring for MACOS you can alternatively utilize the built-in Python debugger, "pdb", for your specific case. PDB will allow you to interactively debug your python script.
In order to do so, you will need to import "pdb" in your Python script and add the calls to "pdb.set_trace()" wherever you need the debugging to start from. Once, the python script is modified, run your MATLAB files as usual.
Note that, when you run your MATLAB script that calls this Python function, MATLAB's Command Window will show the (Pdb) prompt when it hits the pdb.set_trace() line. Once, this is active you can interactively debug using the pdb commands like "n", "s", "c", "l", "p", "q", etc. to inspect the variables, and continue execution.
Here is how your sample python script might look like after adding trace lines for debugging:
import pdb
def my_function():
x = 10
pdb.set_trace()
z = x + 2
return z
Hope this helps!
1 件のコメント
Michal
2024 年 8 月 22 日
Hi Amish
I tried what you proposed above, but got an error as shown on the screenshot below. I tried running pyenv in both "InProcess" and "OutOfProcess" modes but the result was the same.
Do you have any idea how to go around this?
Thanks!

Grace Kepler
2025 年 5 月 9 日
Hi Sammed,
You can install Visual Studio Code and use this debugging workflow: How can I debug Python code using MATLAB's Python Interface and Visual Studio Code.
0 件のコメント
参考
カテゴリ
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!