How to use pdb correctly without executing the whole file first

Question:

This is my .pdbrc file contents:

b 2
c

This is my code.py contents:

print('1st line')
print('2nd line')
print('3rd line')

And when I run this command in terminal:
python3 -m pdb code.py

I get this output:

1st line
2nd line
3rd line
Breakpoint 1 at /Users/mypc/code.py:2
1st line
> /Users/mypc/code.py(2)<module>()
-> print('2nd line')
(Pdb) 

It looks like the whole code.py file is executed first and then the breakpoints are added and applied in the next cycle. How can I make sure that first breakpoints are applied and then the code starts to execute?

Expected output:

Breakpoint 1 at /Users/mypc/code.py:2
1st line
> /Users/mypc/code.py(2)<module>()
-> print('2nd line')
(Pdb) 
Asked By: Rahul R Badenkal

||

Answers:

Name your file (almost) anything other than code.py.

The problem is that code.py is a Python core module:

NAME

code – Utilities needed to emulate Python’s interactive interpreter.

MODULE REFERENCE

https://docs.python.org/3.10/library/code.html

This module is used by pdb, so when you name your file code.py it gets imported during the startup of the pdb module.

Answered By: larsks
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.