How to Debug Python Script (using breakpoints) that is called from Matlab on MAC OS

조회 수: 14 (최근 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?

답변 (2개)

Amish
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
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
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.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by