【问题标题】:Python - print() - debugging -show file and line number [duplicate]Python - print() - 调试 - 显示文件和行号 [重复]
【发布时间】:2017-08-10 18:27:29
【问题描述】:

在 python 中使用 print() 时,是否可以在调用它的地方打印?所以输出看起来像带有 xdebug 的 php 中的 var_dump。例如。我有脚本 D:\Something\script.py,在第 50 行,有一个 print("sometest"),所以输出将如下所示:

D:\Somethinq\script.py:50 sometest

或者有没有什么模块可以做到这一点?在大型项目中,很难管理这些打印件的来源。

【问题讨论】:

    标签: python debugging printing


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-03-29
      • 1970-01-01
      • 2015-10-05
      • 2012-08-03
      • 2021-06-07
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多