w3resource

Initiating Python debugger (pdb) from command line

Start the Python debugger (pdb) from the command line

To start the Python debugger (pdb) from the command line, we need to use the -m pdb option followed by the name of the Python script you want to debug.

The -m pdb option tells Python to run the script under the control of the pdb debugger.

Here's the command to initiate the Python debugger (pdb) from the terminal or command prompt:

python -m pdb your_file_name.py

Replace your_file_name.py with the Python script name you want to debug. When you execute this command, the Python interpreter will launch the script and pause its execution at the first line of the script.

You will see a '(Pdb)' prompt, indicating that you are now in the 'pdb' debugger. At this point, you can use various 'pdb' commands to interactively debug your Python code. These commands include stepping through the code, inspecting variables, setting breakpoints, and more.

Remember to include the 'pdb.set_trace()' statement in your script at the location where you want the debugger to pause. This statement serves as a breakpoint, allowing you to start the debugger at the specified point in the code. If you don't include the 'pdb.set_trace()' statement, the debugger will run the entire script without pausing.

Using 'pdb' is an effective way to identify and fix issues in your Python code, making it a valuable tool for interactive debugging during development.



Follow us on Facebook and Twitter for latest update.