首页 > 解决方案 > tracer.run('main()') 报告名称错误

问题描述

我正在阅读https://docs.python.org/3.7/library/trace.html并获得了演示示例

import sys
import trace

# create a Trace object, telling it what to ignore, and whether to
# do tracing or line-counting or both.
tracer = trace.Trace(
    ignoredirs=[sys.prefix, sys.exec_prefix],
    trace=0,
    count=1)

# run the new command using the given tracer
tracer.run('main()')

# make a report, placing output in the current directory
r = tracer.results()
r.write_results(show_missing=True, coverdir=".")

运行它并得到错误

 NameError: name 'main' is not defined

这样一个愚蠢的例子的目的是什么?

标签: python-3.x

解决方案


您需要有一个名为 main 的函数才能使示例正常工作。在示例中,您正在跟踪函数 main() 的执行,因此需要存在它才能成功运行示例


推荐阅读