How to Debug Python Script (using breakpoints) that is called from Matlab on MAC OS
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Amish
el 22 de En. de 2024
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 comentario
Michal
el 22 de Ag. de 2024
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
el 9 de Mayo de 2025
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 comentarios
Ver también
Categorías
Más información sobre Call Python from MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!