How can I debug Python with Visual Studio and MATLAB's Python Interface

12 ビュー (過去 30 日間)
How can I debug my Python code in using MATLAB's Python Interface and Visual Studio?

採用された回答

MathWorks Support Team
MathWorks Support Team 2025 年 5 月 15 日
編集済み: MathWorks Support Team 2025 年 5 月 15 日
You can use Microsofts Visual Studio to debug your Python code with MATLAB's Python Interface. This process is illustrated in the steps below. Note that for releases R2021b and later, this debugging process only works when using "OutOfProcess" execution mode in MATLAB. See How can I debug Python code using MATLAB's Python Interface and Visual Studio Code  for a workflow that works with both "InProcess" and "OutOfProcess" execution mode in all releases.
Note that Visual Studio is only available for the Windows OS and may require a license. However, the Visual Studio Community edition is freely available for individual developers.
Also, note that, at the time this article is written, Visual Studio only supports Python version 3.7. While other versions of Python may work in Visual Studio, they are not officially supported.
If the project has already been created, select "Open a project or solution" and skip to the next step of these instructions.
Open Visual Studio and select "Create a new project". In the "Create a new project" window, select "Python" for the language, "From Existing Python code" or "Create a new project" , and then "Next".
Follow the steps in the subsequent setup windows, overriding any default settings, as needed.
For this example, we created a project with the following code from the mymod example in the MATLAB documentation.
# 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
For this example, we set a breakpoint at the "return" statement in the  "search" function.
 
2.
>> pyenv ans =   PythonEnvironment with properties:           Version: "3.8"        Executable: "C:\Users\gkepler\AppData\Local\Programs\Python\Python38\python.exe"           Library: "C:\Users\gkepler\AppData\Local\Programs\Python\Python38\python38.dll"              Home: "C:\Users\gkepler\AppData\Local\Programs\Python\Python38"            Status: Loaded     ExecutionMode: InProcess         ProcessID: "24936"       ProcessName: "MATLAB"
In this example, the ExecutionMode is InProcess. If you see Status: NotLoaded, execute any Python command to load the Python interpreter (for example  >> py.list ),  then execute the pyenv command to get the ProcessID for the MATLAB process. In this example, the ProcessID is 24936.
Change directories to correspond to the location of the Visual Studio Python code, or use addpath to add it to MATLAB's path information.
3.
In Visual Studio, select "Attach to Process..." from the "Debug" drop-down menu. In the "Attach to Process" window that opens, choose the "Select" button associated with the "Attach to:" field  and check the box next to "Python".
In the "Attach to Process" window, type "matlab" in the "Available processes" search field. For this example, the selected Process is "MATLABPyHost.exe" and the ID is "24936".
Note that  if you are using OutOfProcess execution mode, you will need to search for a "MATLABPyHost.exe" process.
4.
>> N = py.list({'Jones','Johnson','James'}); >> m = py.importlib.import_module('mymod'); >> py.mymod.search(N);
Execution should stop in Visual Studio at the breakpoint that was set in the "search" function.
 

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by