我使用pyinstaller后,Matplotlib在寻找后端有问题
我正在尝试从.py制作.exe文件,并且我对Matplotlib库有一个问题。当我在vs code plt.show()中运行脚本时,工作正常。但是,当我使用pyinstaller制作.exe并运行.exe文件时,它会给我以下警告:“用户沃宁:matplotlib当前正在使用AGG,这是一个非GUI后端,因此无法显示该图。”直接使用“导入matplotlib.backends.backend_tkagg”导入tkagg也不能解决问题。 可以解决这个问题吗?
I'm trying to make an .exe file from .py and I'm having an issue with matplotlib library. When I run my script in VS Code plt.show() works just fine. But when I use pyinstaller to make an .exe and run the .exe file, it gives me a following warning: "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." Importing TkAgg directly with "import matplotlib.backends.backend_tkagg" doesn't solve the problem either.
Is it possible to fix that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同一问题。
我找到了这篇文章,这可能会有所帮助:
使用AGG,这是非GUI后端,因此无法显示该数字。” 但是,用pyplot绘制pycharm 的图形时
,在我的情况下,简单的解决方案是明确导入后端模块:
Pyinstaller进行了一些相对复杂的分析,Matplotlib本身具有相对复杂的代码,以便能够使用多个GUI后端。我认为总体问题是Pyinstaller并没有看到实际使用任何特定的后端,因此最终不会捆绑任何后端模块。
显式地将导入物变为Pyinstaller,您需要该模块(导入它有效地使用它)。 (也许您只需要一个用于'tkagg'的一个 - 我碰巧正在使用WX GUI,所以我包括两个进口 - 它可以使用。)
I ran into the same issue.
I found this article, which may be helpful:
"UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure." when plotting figure with pyplot on Pycharm
However, the simple fix in my case was to just explicitly import the backend module(s):
Pyinstaller does some relatively sophisticated analysis and Matplotlib itself has some relatively sophisticated code to be able to use multiple GUI backends. I think the overall problem is that Pyinstaller isn't seeing that any particular backend is actually being used and so doesn't end up bundling any backend module.
Explicitly making the import makes it obvious to Pyinstaller that you want that module (importing it is effectively using it). (Maybe you only need the one for 'tkagg' - I happen to be working with a WX GUI, so I included both imports - it works.)