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

27 vues (au cours des 30 derniers jours)
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?

Réponses (2)

Amish
Amish le 22 Jan 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 commentaire
Michal
Michal le 22 Août 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!

Connectez-vous pour commenter.


Grace Kepler
Grace Kepler le 9 Mai 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.

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by