I'm working on system which written mostly in Matlab and partially in Python. Python scripts are called from Matlab. I'm looking for convenient way to debug Python code, called from Matlab, with some IDE (like PyCharm).
I'll be glad to receive advice if it is possible and how. Windows 10, Matlab R2018b, Python 3.6
-
PyCharm is good but I can't imagine what it might mean to open it with MatlabYuval Harpaz– Yuval Harpaz2020年05月10日 09:06:31 +00:00Commented May 10, 2020 at 9:06
-
@SardarUsama: The question is if any IDE is actually able to debug python code by MATLAB. For C/C++ code called form MATLAB we can use MSVC "attach to process", no clue if Python has some similar concepts which actually work here.Daniel– Daniel2020年05月10日 09:58:47 +00:00Commented May 10, 2020 at 9:58
-
Are you on Windows?Paolo– Paolo2020年05月10日 10:32:48 +00:00Commented May 10, 2020 at 10:32
-
@Daniel PyCharm has an ability to attach to process. But how can I know to which process to attach ?ivbsd1– ivbsd12020年05月10日 11:03:43 +00:00Commented May 10, 2020 at 11:03
-
@Paolo Yes, Windows 10ivbsd1– ivbsd12020年05月10日 11:05:02 +00:00Commented May 10, 2020 at 11:05
1 Answer 1
Here's a solution to debug your Python code, executed from MATLAB, in Microsoft Visual Studio.
This solution was tested on Windows 10, with Python 3.6, MATLAB R2020a and Visual Studio 2015.
Create your .py file:
# mymod.py
"""Python module demonstrates passing MATLAB types to Python functions"""
def search(words):
"""Return list of words containing 'son'"""
newlist = [w for w in words if 'son' in w]
return newlist
def theend(words):
"""Append 'The End' to list of words"""
words.append('The End')
return words
In MATLAB, import the module and create a dummy list:
>> mod = py.importlib.import_module('mymod');
>> N = py.list({'Jones','Johnson','James'});
Open Visual Studio and create a new Python project from existing code. Then, select Attach to Process from the Debug menu:
Search for MATLAB:
select the MATLAB process and attach.
Place a breakpoint in your code:
Now go back to MATLAB and invoke the function:
>> py.mymod.search(N)
MATLAB command window will halt. Go to Visual Studio and debug your code: