Python-print()-调试-显示文件和行号

Python-print()-调试-显示文件和行号,python,debugging,printing,Python,Debugging,Printing,在python中使用print()时,是否可以打印调用它的位置?因此,输出将类似于带有xdebug的php中的var_dump。例如,我有脚本D:\Something\script.py,在第50行有一个打印(“sometest”),因此输出如下所示: D:\Somethinq\script.py:50 sometest 或者是否有任何模块可以实现这一点?在大型项目中,很难管理这些打印的来源。因此,使用中提供的答案,可以调用此函数而不是print(),并在每次输出之前打印行号: from in

在python中使用print()时,是否可以打印调用它的位置?因此,输出将类似于带有xdebug的php中的var_dump。例如,我有脚本D:\Something\script.py,在第50行有一个打印(“sometest”),因此输出如下所示:

D:\Somethinq\script.py:50 sometest

或者是否有任何模块可以实现这一点?在大型项目中,很难管理这些打印的来源。

因此,使用中提供的答案,可以调用此函数而不是print(),并在每次输出之前打印行号:

from inspect import currentframe

def debug_print(arg):
    frameinfo = currentframe()
    print(frameinfo.f_back.f_lineno,":",arg)