如何在VSCode的Python Interactive面板中调试Python模块?
Absolutely! You can absolutely set breakpoints in your Python module code and have execution pause when triggered via the VS Code Python Interactive panel (the Jupyter-like component). Here's a step-by-step guide to make this work smoothly:
Add breakpoints to your module code
Navigate to the failing function in your Python module, then click the blank space to the left of the line number where you want execution to pause—you’ll see a red dot pop up, marking an active breakpoint. You can also use the keyboard shortcutF9to toggle breakpoints on/off for the current line.Make sure your VS Code Python extension is up to date
Outdated versions might have compatibility issues with interactive debugging. Open the Extensions panel (Ctrl+Shift+X), search for "Python", and hit "Update" if a newer version is available.Launch the Jupyter Interactive Debugging session
- Open the Debug panel using
Ctrl+Shift+D(or click the bug icon in the left sidebar). - From the dropdown menu at the top of the Debug panel, select Python: Jupyter Interactive Debugging. If this option isn’t listed, click "create a launch.json file", choose "Python" as the environment, then pick the Jupyter debugging configuration from the options.
- Click the green "Start Debugging" button (or press
F5) to kick off the debugging session.
- Open the Debug panel using
Trigger the function call in the Interactive panel
Head back to your Python Interactive panel and re-run the code block that calls your problematic function. When execution hits your breakpoint, VS Code will automatically switch to the Debug view, where you can inspect variables, step through code line-by-line, and figure out what’s going wrong.
Quick Tip
If you’ve edited your module since importing it, run import importlib; importlib.reload(your_module_name) in the Interactive panel first. This ensures you’re debugging the latest version of your code, avoiding issues with cached old code.
内容的提问来源于stack exchange,提问作者oshi2016

