这种情况可能是由于Bazel测试无法在非交互式模式下使用matplotlib所致。解决方法是在test rule中添加以下参数:
test(name = "test_name",
srcs = ["test.py"],
args = ["--matplotlib=agg"],
deps = [
":module_to_test",
"@org_python//matplotlib:matplotlib",
],
)
--matplotlib=agg
参数将Matplotlib后端设置为agg
,即非交互式后端,这可以使测试在没有图形界面的情况下运行并生成图形。