matplotlib 报ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running问题

小白duzw / 2024-08-28 / 原文

当使用linux系统,没有UI界面时,缺少一些UI库的时候,

使用matplotlib库时,可以设置

import matplotlib
matplotlib.use('Agg')  # 使用 Agg 后端

当第三方库使用到matplotlib库,不方便修改第三方库时,

可以安装虚拟服务

sudo apt install xvfb

xvfb-run python your_script.py