如何打印脚本的每一行,因为它仅针对正在运行的顶级脚本运行? [英] How to print each line of a script as it is run only for the top-level script being run?

查看:41
本文介绍了如何打印脚本的每一行,因为它仅针对正在运行的顶级脚本运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python 的跟踪模块将允许您运行一个脚本来打印每一行代码,因为它在脚本和所有导入的模块中都运行,如下所示:

python's trace module will allow you to run a script printing each line of code as it is run in both the script and all imported modules like so:

 python -m trace -trace myscript.py

有没有办法做同样的事情,但打印顶级调用,即只在运行时打印 myscript.py 中的行?

Is there a way to do the same thing, but only print the top-level calls, i.e. only print the lines in myscript.py as they are run?

我正在尝试调试中止陷阱失败,但我无法弄清楚它在哪里死亡.不幸的是,使用完整的 --trace 需要很长时间 - 脚本通常需要 2-3 分钟才能运行,而完整的跟踪已经持续了几个小时.

I am trying to debug an abort trap failure and I can't figure out where it's dying. Unfortunately, using the full --trace takes forever - the script normally takes 2-3 minutes to run, and the full trace has been going for hours.

推荐答案

我偶然发现了这个问题,发现 grep 是一个快速而肮脏的解决方案:

I stumbled into this problem and found grep to be a quick and dirty solution:

python -m trace --trace my_script.py | grep my_script.py

不过,我的脚本在有限的时间内运行.这可能不适用于更复杂的脚本.

My script runs in finite time though. This probably won't work well for more complex scripts.

这篇关于如何打印脚本的每一行,因为它仅针对正在运行的顶级脚本运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆