DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk6 min

How to Debug Python Code: A Practical Guide to Tracebacks, pdb, and VS Code

Reproduce the failure, use the traceback to narrow the cause, inspect runtime values with pdb or VS Code, then rerun the same case to confirm the fix.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To debug Python code, reproduce the failure, read the traceback to find the failing path, then inspect the values at a breakpoint near the suspected cause. For a quick check, use a print statement or assertion; for changing state or multi-step behavior, use Python’s built-in pdb debugger or the Python debugger in VS Code. Change one thing at a time and rerun the same case to verify the fix.

Start with a reproducible failure

Before changing code, record the input and the steps that trigger the problem. Run those steps again and confirm that the failure is consistent. If practical, reduce the failing case to the smallest input and the fewest relevant lines; less unrelated state makes the cause easier to isolate.

Keep the complete traceback. It shows the exception type and message as well as the route through calls that led to the failure. The line where execution stopped is an important clue, but it is not always where the underlying mistake began: an incorrect value may have been produced earlier and passed through several functions.

Read the traceback and form a specific question

  1. Start at the bottom of the traceback: note the exception type and its message.
  2. Look upward through the application frames to identify the relevant function calls and the line where the exception was raised.
  3. Ask what value or condition would make that line fail, then compare the expected value with what the program actually has.

Do not begin by stepping through every line of a large program. Pick a focused question, such as “What is in this variable immediately before this division?” or “Which call supplied this argument?” Then choose the smallest inspection method that can answer it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

Choose a debugging method

Situation Good starting point Why
A small script, terminal session, or learning debugger commands pdb It is included with Python and supports breakpoints, stepping, frame inspection, and post-mortem debugging.
You prefer visual breakpoints or already work in VS Code VS Code Python debugger You can set breakpoints in the editor and inspect paused execution in the IDE; a launch configuration is available for scripts that need a chosen entry point or settings.
The program has already crashed and you need its exception context pdb post-mortem mode The command-line debugger can enter post-mortem debugging after an abnormal exit, and the pdb module provides post-mortem functions.

Debug from the terminal with pdb

Python’s standard-library pdb is an interactive source-code debugger. The built-in breakpoint() function is a convenient way to pause at a chosen line; by default, it enters pdb.

Pause at a line in your script

Place breakpoint() immediately before the code you want to inspect, then run the script as usual:

def average(values):
    breakpoint()
    return sum(values) / len(values)

print(average([2, 4, 6]))

When execution pauses at the (Pdb) prompt, try p values to evaluate and print the current value of values. Use next to run the next statement without entering a called function, step to enter a called function, and continue to resume execution until another breakpoint or program exit. Use where to inspect the stack and list to view nearby source.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Inspect the value you suspected, then step only as far as needed to test your hypothesis. If the value is already wrong when execution reaches the breakpoint, move the breakpoint earlier in the relevant path and inspect where it changes.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Run a script under pdb without editing it

To start a script under the command-line debugger, run:

python -m pdb script.py

Replace script.py with the path to your file. The debugger also supports module execution with -m. Python 3.14 added -p / --pid for attaching to a process; use that option only where Python 3.14 or later is available. When a program exits abnormally under command-line pdb, it automatically enters post-mortem debugging.

Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

Inspect a crash after it happens

Post-mortem debugging lets you inspect the stack and frame state associated with an exception after it has been raised. The pdb documentation describes pdb.pm() and pdb.post_mortem(); command-line pdb also enters this mode automatically after an abnormal exit. Use the stack commands and expression inspection to trace the relevant values back through the failing call path.

Remove or disable a breakpoint when finished

Remove temporary breakpoint() calls when the investigation is complete. For a run where the built-in breakpoint hook should do nothing, set PYTHONBREAKPOINT=0 in the environment. This is useful when a program must run non-interactively.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Debug a Python file in VS Code

For a single script, the official VS Code Python debugging instructions give this entry point: select the dropdown next to Run, then choose Python Debugger: Debug Python File. Set a breakpoint in the editor at or near the suspected fault, start debugging, and inspect the paused program in the IDE.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

If the script needs a specific entry point or launch settings, use a launch.json configuration in the .vscode directory. Keep the Python interpreter selected in VS Code aligned with the environment in which the project’s dependencies are installed; otherwise, the debugger may run under an environment that does not match the one you expected.

Use print statements and assertions for simple checks

A debugger is not required for every bug. If one value at one point is in doubt, a temporary print statement can reveal what the program received. An assertion can make an expected condition explicit and fail at the point where that condition stops being true. When state changes across several calls, a breakpoint is usually more useful because it lets you inspect the surrounding frame and call stack without adding repeated output.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Verify the correction

  1. Make one targeted change based on the evidence you observed.
  2. Run the same input and steps that reproduced the original failure.
  3. Check that the result now matches what you expected.
  4. Run any related cases needed to check that the change has not introduced a different failure.

A debugger helps test a hypothesis; it does not prove a fix on its own. The reproduction is what lets you verify the result.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

Troubleshoot common debugging snags

Symptom Likely cause What to do
The traceback points to a line that looks reasonable. The bad value may have been created in an earlier call. Inspect the relevant application frames and move a breakpoint earlier to find where the value first diverges from expectation.
breakpoint() does not open an interactive prompt. The built-in breakpoint hook may have been disabled, including with PYTHONBREAKPOINT=0, or the program may be running without an interactive terminal. Check the environment and run the program in an interactive terminal when using the default pdb hook.
The debugger runs code with missing or unexpected dependencies. The selected VS Code interpreter may not match the project environment. Select the interpreter associated with the environment where the project dependencies are installed, then start the debug session again.
Stepping through the program is taking too long. The inspection has no specific question, or the breakpoint is too far from the suspected fault. Set a breakpoint closer to the relevant operation and inspect only the values and calls needed to test the current hypothesis.

Or skip the browser setup

ScreenshotNeo is a website screenshot API, not a Python debugger. If your Python issue involves a page your code loads or automates, a screenshot can provide visual context alongside your logs and debugger inspection. One GET request returns an image or PDF; the example below saves a WebP screenshot of a page:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation. Cookie and consent banners are accepted as a visitor and removed before capture, along with 60+ known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers report the page verdict and billing status. Its MCP server gives AI agents tools for screenshots, page information, and PDFs. The Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots. Learn about ScreenshotNeo or sign up free for 1,000 screenshots a month with no card.

Frequently Asked Questions

Can I debug Python without installing anything?

Yes. pdb is included in Python’s standard library, and the built-in breakpoint() function can send execution to it by default.

What is the difference between step and next in pdb?

step enters a function call; next runs past the call and pauses at the next line in the current frame.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.