0

Is it possible to debug the code of a function that was defined via exec in a convenient way?

foo_source = """\
def foo():
    print("start")
    breakpoint()
    print("end")
"""

exec(foo_source)
foo()

The breakpoint in the code works, and I can step through the function, but I don't see the code listing.

$ python foo.py
start
> <string>(4)foo()
(Pdb) list
[EOF]
(Pdb) next
end
--Return--
> <string>(4)foo()->None
(Pdb)
Jakube
  • 3,353
  • 3
  • 23
  • 40
  • `exec` is very rarely a good idea. Unless you have a truly amazing justification for using it, I'd recommend any other solution. Not `eval`, either. – Carbon Jul 04 '23 at 20:16

0 Answers0