3

When using print() in python, is it possible to print where it was called? So the output will look like var_dump in php with xdebug. Eg. I have script D:\Something\script.py, and at line 50, there is a print("sometest"), so the output will look like this:

D:\Somethinq\script.py:50 sometest

Or is there any module that could achieve this? In large projects, it's really hard to manage where these prints came from.

Adam Ježek
  • 464
  • 5
  • 15

1 Answers1

12

So, using answers provided in filename and line number of python script , this function can be called instead of print(), and prints line number before every output:

from inspect import currentframe

def debug_print(arg):
    frameinfo = currentframe()
    print(frameinfo.f_back.f_lineno,":",arg)
Adam Ježek
  • 464
  • 5
  • 15