VS Code

How to Debug Python in VSCode for Beginners

How to Debug Python in VSCode for Beginners

Breakpoints in VS Code only pause a Python script when the debugger is attached, and the button most people click first doesn’t attach it. Put a breakpoint on a line, open the small dropdown next to the Run button, and pick Python Debugger: Debug Python File. Once the program stops, F10 and F11 walk you through it.

There’s nothing extra to install beforehand. The Python Debugger extension has installed by default alongside the Python extension since February 2024, according to the Python team’s release notes.

The Run Python File button sitting right beside that dropdown is a different thing. Per the VS Code documentation, it only opens a terminal and runs the script. No debugger gets attached, so breakpoints never pause anything during a run like that.

What do you need before you can debug Python in VSCode?

YouTube player

Python itself has to be on the machine first. Microsoft’s VS Code Python tutorial says plainly that the extension doesn’t include Python, so the interpreter goes in before anything else.

  • A Python 3 interpreter. On Windows that’s the latest python.org release. On macOS, use Homebrew, because the tutorial treats the system Python on macOS as unsupported.
  • The Python extension (ms-python.python), which adds IntelliSense, running and debugging to the editor
  • The Python Debugger extension, which comes along automatically with the Python extension and handles debugging through debugpy for scripts, web apps and remote processes

If you want to check that the debugger extension actually landed, open the Extensions view (Ctrl+Shift+X on Windows and Linux) and search for it.

It helps to know how the pieces talk to each other, since it explains a lot of the error messages later. VS Code doesn’t run your script itself. debugpy implements the Debug Adapter Protocol for Python 3, so the editor sends breakpoint and step requests and debugpy reports back where the program paused.

Pylance underlines syntax errors and type problems before the script runs. The debugger is for what the editor can’t see, which is wrong values at runtime.

Selecting the interpreter

Look at the status bar. The selected interpreter shows there, and clicking it opens the list (the Python: Select Interpreter command in the Command Palette does the same job).

If you never pick one, the extension goes for workspace-local environments (.venv, venv) first and global interpreters after that. In practice, a project folder with a .venv in it usually debugs under that venv without you touching anything.

If the status bar names the wrong one, changing the Python interpreter in VSCode takes a couple of clicks.

The debugger follows whatever environment is selected. You can override that for one configuration with the python property in launch.json. Broken venvs are easy to spot, too. VS Code labels an environment “(broken)” when pyvenv.cfg is missing or the Python executable is invalid.

Multi-project workspaces have a catch (as of the 2026 documentation). Per-project environments apply to running and debugging, but Pylance still uses a single interpreter per workspace, so editor warnings and debug behavior can disagree. That mismatch can eat a surprising amount of time if you don’t know about it.

How do you start a debug session on a Python file?

YouTube player

Open the dropdown on the Run button and choose Python Debugger: Debug Python File. Microsoft’s documentation calls this the simplest way to debug a single script, and you don’t need a launch.json for it.

F5 works as well. The first press opens a configuration menu where you pick Python File, and that configuration in the Run and Debug view runs whichever file is open.

  1. Save the script and check the interpreter in the status bar.
  2. Click the margin left of a line number to set a breakpoint, or press F9.
  3. Open the Run button’s dropdown and select Python Debugger: Debug Python File (or press F5 and pick Python File).
  4. Wait for the program to pause on the breakpoint line. The debug toolbar appears as soon as the session starts.
  5. Step with F10 or F11, or press F5 to run to the next breakpoint.
  6. Press Shift+F5 to end the session.

Tutorials don’t agree on the name, which gets annoying when you’re following one. Microsoft’s own tutorial says “Python File” in one place and “Python: Current file” in another, and older posts use both. Same thing either way. It runs the open file under the debugger.

Why the green Run arrow ignores breakpoints

The Run Python File button opens a terminal, activates your interpreter and runs the script. That’s all it does. Nothing is attached, so the red dots in the gutter just sit there.

Beginners hit this on day one, constantly. The button is plain execution, the same result you’d get from running Python in VSCode through the terminal. If you want breakpoints to hit and the debug toolbar to show up, use Debug Python File.

How do breakpoints work, and which type should you use?

Execution stops just before the marked line runs. A standard breakpoint does that every single time. Conditional, hit-count, logpoint and exception breakpoints change when the pause happens, or whether it happens at all.

TypePauses whenBest for
StandardExecution reaches the lineFirst look at a suspect line
ConditionalAn expression evaluates to trueOne bad value inside a loop
Hit countThe line has run N timesFailure on a late iteration
LogpointNever, it only logsChecking a value without editing code
ExceptionAn exception is raised or uncaughtA crash with an unknown source

Click the margin, or press F9 on the current line, to set a standard one. A filled red circle means it’s on and a filled gray circle means it’s disabled. Watch for the hollow gray circle. That one means the debugger couldn’t register the breakpoint, often because the line holds no executable code.

Breakpoints on non-executable lines, such as a pass statement, move to the nearest valid line when the debugger runs.

You can also pause from code. The built-in breakpoint() (PEP 553) works under debugpy where available. If another handler overrides it, debugpy.breakpoint() covers that case, and it only pauses if a debugger is attached.

Conditional and hit-count breakpoints

Python breakpoints can trigger on an expression, a hit count, or a combination of both. An expression like order.total < 0 pauses only on the bad record. Hit counts take integers, or integers preceded by ==, >, >=, <, <= and %.

The documentation’s own example uses a hit count of >5, which pauses after five occurrences. I like %100 for long loops. It stops on every hundredth pass, so a loop that runs thousands of times doesn’t turn into a clicking marathon.

If you know the bad value, write a condition. If you only know roughly which iteration breaks, go with a hit count.

Logpoints

A logpoint writes a message to the Debug Console and keeps going. Expressions go in curly braces, as in User ID: {user.id}, and the icon is a diamond.

For a value you’d print and delete later, this is the better habit. You never edit the source, so there’s nothing to clean up before a commit.

The downside is pretty obvious once you think about it. Nothing pauses, so you can’t look at any other variable at that moment.

Exception breakpoints

The Breakpoints panel has its own exception filters, and they don’t behave the same way.

Raised Exceptions triggers whenever an exception is raised or re-raised, and with justMyCode at its default of true, only for user code. It starts unchecked. Uncaught Exceptions fires when the exception isn’t handled anywhere, and it’s the only filter checked out of the box.

User Uncaught Exceptions also starts unchecked. It fires when an exception raised in user code would cross into library code, which means it also catches errors a library handles quietly.

The justMyCode part matters more than it looks. The VS Code documentation sets justMyCode to true by default, and the debugpy FAQ ties Raised Exceptions to that setting, so exceptions inside libraries stay hidden until you change it.

My advice is to leave the default alone and run with Uncaught Exceptions only. If you turn on User Uncaught Exceptions and the debugger starts stopping on errors that a library swallows anyway, there’s a debugpy GitHub thread from 2021 (when that filter was briefly on by default) where unchecking it was the fix.

How do you step through code line by line?

F10 runs the current line without going into any function calls on it. F11 steps into the function called on that line, and Shift+F11 finishes the current function and pauses back in the caller. F5 resumes until the next breakpoint. Those are the Windows and Linux shortcuts on the VS Code debug toolbar.

Restart (Ctrl+Shift+F5) stops and relaunches with the current configuration, and Stop (Shift+F5) ends the session.

Choosing between them is mostly about trust. Step Into your own functions. Step Over anything you trust, like print calls and library calls.

Stepping into library code usually goes nowhere by default, because justMyCode defaults to true. Setting it to false lets you step into standard library code.

Here’s a typical session on a loop that totals discounted prices. Put the breakpoint on the total += apply_discount(price) line. F11 takes you into apply_discount, where you read the wrong return value. Shift+F11 climbs back out, and F10 moves on to the next iteration.

How do you inspect variables, the call stack and expressions while paused?

Most of it sits in the Run and Debug sidebar, where you’ll find the Variables, Watch and Call Stack panels. The Debug Console opens separately with Ctrl+Shift+Y on Windows and Linux.

Variables shows local and global variables for whichever stack frame is selected. Right-click one and choose Set Value (F2) to change it. That edits the running program, not your source file, which is handy for testing a theory.

Watch evaluates expressions every time the debugger pauses. Add something like len(items) and it updates on each step.

One trap there: a broken expression can stall the panel. The Python debugging docs advise clearing invalid expressions from Watch and restarting the debugger.

The Call Stack panel lists the active function calls, and selecting a frame shows its source and variables. When an exception stops you deep inside some helper, this is where you go. Climb the frames until you find the caller that passed the bad argument.

And the Debug Console is a REPL that evaluates expressions in the context of the selected stack frame. It isn’t the integrated terminal. Shift+Enter adds lines for multiline input.

Which one to reach for? Hovering over an expression in the editor shows its current value, and that’s the quickest check of all for one value, once. For the same expression across many pauses, put it in Watch. If you want to test a fix before touching the file, type it into the Debug Console.

How does launch.json control a debug session?

VS Code keeps debug configurations in a launch.json file inside a .vscode folder in your workspace. Each entry tells the debugger what to run and how.

Honestly, skip it until you need it. For a plain script, a hand-edited launch.json is one more thing that can break. It starts earning its place once you need arguments, environment variables, a fixed working directory or a framework target.

To create one, click the create a launch.json link in the Run and Debug view or use the Run > Open Configurations menu. The Python Debugger extension then writes a file containing the template you picked, such as Python File.

The type field has changed over time, and old tutorials show it. In February 2024, Microsoft’s release notes told users to replace "type": "python" with "type": "debugpy". By May 2024, both values were interpreted as references to the Python Debugger extension, and debugpy became the recommended value. The 2026 documentation lists “python” as deprecated in favor of “debugpy”.

So a config copied from an older tutorial still launches. VS Code shows a message that the configuration will be deprecated soon. Just don’t carry the old value into new ones.

SettingDefault or typical valueWhat it controls
program${file} for the open fileEntry script to run
argsList of stringsCommand-line arguments passed to the script
cwd${workspaceFolder}Working directory for the run
consoleintegratedTerminalWhere output appears (internalConsole, integratedTerminal, externalTerminal)
justMyCodetrueWhether library frames are skipped

You’ll also run into stopOnEntry fairly quickly. It pauses on the first line when set to true. env and envFile set environment variables inline or from a file.

Passing arguments and setting the working directory

args takes one string per command-line token, such as ["--input", "data.csv"]. Set it to ${command:pickArgs} and VS Code prompts for arguments on every run.

cwd is the one that bites people. A script that opens data.csv by a relative path looks for it in the working directory, so a wrong cwd gives a file-not-found error from code that looks perfectly fine.

Secrets and per-machine values belong in env or envFile, not in the script itself.

With console left on its default, script output shows up in the same panel as the VS Code terminal you already use.

When to turn justMyCode off

Turn it off when the bug lives inside a library you call, not in your own code.

One detail trips people up across projects. Since the October 2023 release, a new launch.json takes its justMyCode value from the debugpy.debugJustMyCode user setting unless the file specifies one. If two projects behave differently, check that user setting first.

When you’re done inspecting the library, switch it back. Otherwise Step Into keeps landing in code you didn’t write.

How do you debug tests, web apps and running processes?

The setup shifts a little depending on what you’re debugging. Tests go through the Testing view. Web apps want a framework configuration, while a process you didn’t launch from VS Code needs an attach configuration.

Debugging pytest tests

The Python extension supports unittest and pytest, with 7.0.0 as the minimum pytest version. When both are enabled, only pytest runs.

The quick way in is to right-click the gutter decoration beside a test and choose Debug Test, or click the Debug Test icon in Test Explorer. For more control, add "purpose": ["debug-test"] to a configuration. That decides how Test: Debug All Tests, Debug Tests in Current File and Debug Test at Cursor launch.

When several configurations carry that purpose, the first one is used. If tests don’t appear in Test Explorer at all, that’s a discovery problem, so sort it out first by running pytest in VSCode normally.

Flask, Django and FastAPI apps

Flask uses "module": "flask" instead of program, with FLASK_APP in env and args set to ["run", "--no-debugger"]. Add "jinja": true for Jinja templates. The --no-debugger flag switches off Flask’s own interactive debugger so only VS Code’s is attached.

A common Flask snag: an FLASK_APP value of ${workspaceFolder}/app.py can trigger “Cannot import module C” errors (where C is a drive letter). The docs say to use the filename only.

Django is simpler. Set program to manage.py, args to ["runserver"], and add "django": true for Django HTML templates.

FastAPI is the odd one out. The debugging page gives no FastAPI-specific configuration and points to the separate FastAPI tutorial instead. Otherwise, use the generated entries under Show all automatic debug configurations.

Attaching to a running process

  1. Install debugpy in the target environment with python -m pip install --upgrade debugpy.
  2. Start the script with python -m debugpy --listen 5678 ./myscript.py; the default host is 127.0.0.1.
  3. Add an attach configuration with "type": "debugpy", "request": "attach" and a connect block pointing at localhost and port 5678.
  4. Start that configuration from the Run and Debug view.

Adding --wait-for-client keeps the script from running until a debugger connects. To hook into a process that’s already running, debugpy accepts a --pid option.

Be careful on remote machines. Any host other than 127.0.0.1 or localhost opens a port to every machine that can reach it, so the docs recommend SSH tunnels instead. Tunnel examples add pathMappings with localRoot and remoteRoot.

Microsoft’s own examples are a bit inconsistent here. The network attach uses a connect object, while the SSH tunnel example uses top-level host and port keys, and the docs don’t explain the difference.

On Linux, a “timed out” error on attach may require temporarily setting /proc/sys/kernel/yama/ptrace_scope to 0.

Why is the debugger skipping your breakpoints?

Usually it’s the wrong interpreter or a disabled Python Debugger extension. If neither of those, look at coverage plugins and the justMyCode filter. The first two take seconds to check, so start there.

Interpreter and extension problems

The troubleshooting notes say to confirm the Python Debugger extension is installed and enabled, and to check the interpreter path with Python: Select Interpreter.

A vscode-python-debugger GitHub issue describes a variant where the session stopped immediately with the system Python but worked under a .venv. Select the project’s venv interpreter and start a fresh session.

Coverage tools and unsaved test files

This failure is documented from both sides. The VS Code testing docs say that with pytest-cov installed, the debugger may not stop at breakpoints because pytest-cov uses the same technique.

The pytest-cov docs explain why. Debuggers and coverage both rely on sys.settrace, and coverage does not work well with other tracers running at the same time. (That page names PyCharm and “other IDEs” but never mentions VS Code.)

The fix is to turn code coverage off for debug runs. Add --no-cov to pytestArgs, or set "env": {"PYTEST_ADDOPTS": "--no-cov"} in the debug configuration.

Save the test file before debugging too. Running or debugging a test doesn’t save the file for you.

When justMyCode false is ignored

Sources genuinely disagree on this one.

The original report says that on Python 3.12 and 3.13, justMyCode false is ignored when debugging pytest tests, so a breakpoint inside the requests library never triggers. Python 3.11 and earlier stop as expected.

The maintainer replied that the reporter was on the old test discovery structure, that the new one is the default from Python extension 2025.0.0, and that it worked in her local testing. Later, people on WSL, Windows and SSH-remote macOS with Python 3.12 reported the problem persisting.

So the difference seems to follow the test discovery path, not the setting itself. The issue page shows no closure, and users disagree about whether it’s fixed.

If a library breakpoint stays silent during tests, try the same call from a plain script configuration. The report only covers test debugging.

Should you use the VSCode debugger, print() or breakpoint()?

Use print() when you need one value once. When the bug depends on state across several lines or function calls, the debugger is worth the setup.

The built-in breakpoint() sits somewhere in between. No editor setup, but you’re working in a command-line interface.

print()

It takes zero setup and works anywhere, including a server log. Output also stays on screen after the run ends.

The cost shows up later. Every extra value means another edit and another rerun, and stray prints have a way of ending up in commits.

breakpoint() and pdb

The built-in arrived in Python 3.7. It calls sys.breakpointhook(), which calls pdb.set_trace() by default, so you don’t need to import pdb.

The nice part is the PYTHONBREAKPOINT environment variable, which changes its behavior without a code edit. A replaced hook can also drop you into a different debugger.

But pdb is a command-line interface with no variable or call stack panels. And if sys.breakpointhook() has been replaced, the documented behavior isn’t guaranteed.

The VSCode debugger

It costs the most to set up and gives the most back. You can inspect any variable or stack frame without touching the source, and conditional, hit-count, logpoint and exception breakpoints narrow a pause down to the one iteration that matters. Nothing to delete before a commit, either.

It does need a working interpreter and configuration, and a process you can’t launch from VS Code needs the attach route.

A rule that holds up in practice: start with print() for a single value, and switch to the debugger the second time you add a print() for the same bug.

Python Debugging in VSCode FAQ

Can I debug a Jupyter notebook cell in VSCode?

It works in the Python Interactive window. Each cell shows a Debug Cell CodeLens above it, and you set a breakpoint in the margin before clicking it.

By default, Debug Cell steps only into your own code. Unchecking Debug Just My Code in the Jupyter extension settings changes that.

Can I debug Python on a remote machine over SSH?

Yes. With the Remote – SSH extension, you select a launch configuration in launch.json and press F5. The application starts on the remote host with the debugger attached to it.

How do I debug multi-threaded code?

Mostly the same way as anything else. Threads started through Python’s own threading APIs need nothing special.

Threads created through native thread APIs, such as the Win32 CreateThread function, are the exception. Those need debugpy.debug_this_thread() placed after import debugpy at the top of the file.

Does the debugger follow subprocesses my script starts?

Not by default. The subProcess setting is false until you turn it on, so add “subProcess”: true to the launch.json configuration.

Can the debugger reload my code changes without a restart?

The autoReload setting handles that. Set it to {“enable”: true} and code changes reload after a breakpoint hit.

Which Python versions does the VSCode debugger support?

All actively supported Python versions work with the Python Debugger extension. Older projects need an older extension build.

According to the extension’s README, a Python 3.8 project tops out at the 2024.2 builds, while Python 3.9 reaches 2025.5. The maintainers advise upgrading Python when possible.

Making Python Debugging in VSCode a Habit

Add one skill per practice session and it sticks. Start with a single breakpoint on a script you wrote yourself, then get comfortable with F10 and F11.

After that, try a conditional breakpoint or a logpoint inside a loop. Leave launch.json alone until a script actually needs arguments or environment variables. Each step takes away one more reason to fall back on print().

Configuration comes last on purpose, since a hand-written launch.json adds a failure point before you’ve found a single bug.

print() will still feel faster for a while, because a debug run costs more to start. That lasts until the first bug that spans several function calls.

This order holds while Debug Python File remains the simplest route for a single script, a position verified against Microsoft’s documentation in November 2026.

Once a bug is fixed, unit tests keep it from coming back.

Bogdan Sandu

Stay sharp. Ship better code.

Every week: one curated article, one tool worth knowing, one tip you can use tomorrow. No noise, no padding.