简体   繁体   English

py2exe `ImportError: No module named backend_tkagg`

[英]py2exe `ImportError: No module named backend_tkagg`

I am trying to make a windows executable from a python script that uses matplotlib and it seems that I am getting a common error.我正在尝试从使用 matplotlib 的 python 脚本制作一个 Windows 可执行文件,但似乎我遇到了一个常见错误。

File "run.py", line 29, in import matplotlib.pyplot as plt File "matplotlib\\pyplot.pyc", line 95, in File "matplotlib\\backends__init__.pyc", line 25, in pylab_setup ImportError: No module named backend_tkagg文件“run.py”,第 29 行,导入 matplotlib.pyplot 作为 plt 文件“matplotlib\\pyplot.pyc”,第 95 行,文件“matplotlib\\backends__init__.pyc”,第 25 行,pylab_setup 导入错误:没有名为 backend_tkagg 的模块

The problem is that I didn't found a solution while googling all over the internet.问题是我在互联网上搜索时没有找到解决方案。

Here is my setup.py这是我的setup.py

from distutils.core import setup
import matplotlib
import py2exe 
matplotlib.use('TkAgg')
setup(data_files=matplotlib.get_py2exe_datafiles(),console=['run.py'])

First, the easy question, is that backend installed?首先,简单的问题,是否安装了后端? On my Fedora system I had to install it separately from the base matplotlib.在我的 Fedora 系统上,我必须将它与基础 matplotlib 分开安装。

At a Python console can you:在 Python 控制台中,您可以:

>>> import matplotlib.backends.backend_tkagg

If that works, then force py2exe to include it.如果可行,则强制 py2exe 包含它。 In your config:在您的配置中:

opts = {
  'py2exe': { "includes" : ["matplotlib.backends.backend_tkagg"] }
}

If you are using py2exe it doesn't handle .egg formatted Python modules.如果您使用的是 py2exe,它不会处理 .egg 格式的 Python 模块。 If you used easy_install to install the trouble module then you might only have the .egg version.如果您使用 easy_install 安装故障模块,那么您可能只有 .egg 版本。 See the py2exe site for more info on how to fix it.有关如何修复它的更多信息,请参阅 py2exe 站点。

http://www.py2exe.org/index.cgi/ExeWithEggs http://www.py2exe.org/index.cgi/ExeWithEggs

This works well这很好用

from distutils.core import setup import py2exe, sys, os import matplotlib从 distutils.core 导入设置导入 py2exe、sys、os 导入 matplotlib

sys.setrecursionlimit(12000) sys.argv.append('py2exe') sys.setrecursionlimit(12000) sys.argv.append('py2exe')

setup( options = { "py2exe" : { "bundle_files":3, "compressed":True, "includes" : ["matplotlib.backends.backend_tkagg"] } }, windows = [{"script": "script.py"}], setup( options = { "py2exe" : { "bundle_files":3, "compressed":True, "includes" : ["matplotlib.backends.backend_tkagg"] } }, windows = [{"script": "script.py "}],

zipfile = None,

data_files=matplotlib data_files = matplotlib.get_py2exe_datafiles(), ) data_files=matplotlib data_files = matplotlib.get_py2exe_datafiles(), )

Run the following command to install the backend_tkagg运行以下命令安装backend_tkagg

For centos -- sudo yum install python-matplotlib-tk对于 centos -- sudo yum install python-matplotlib-tk

This should work.这应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM