mlbox ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running

真是巨大的坑,按照网上说的,matplotlib.use('agg'),修改matplotlibrc文件中的backend,plt.switch_backend('Agg'),%matplotlib inline统统失效,包括在启动Jupyter时设置环境变量export MPLBACKEND=Agg,在运行Reader(sep=',').train_test_spilt(paths, target_name)时,依然会报错。
之后看了一下报错的堆栈,在执行这一句时,应该是启动了新的进程,所以,在jupyter中无论怎么设置maplotlib,在新启动的进程中,这个设置无法生效,万般无奈下,按照报错的其中一个文件python3.8/site-packages/mlbox/prediction/predictor.py,打开一看,瞬间气晕了,15行开始写了如下的语句

try:
    matplotlib.use('TkAgg')
except:
    pass

所以,这就难怪会报标题那样的错了,把这句话注掉,或者改成matplotlib.use('agg'),重启kernel,一切完美!

原文地址:https://www.cnblogs.com/stevenluo/p/14723704.html