Python 导入错误:没有带有 maptlotlib 1.3.0 和 py2exe 的名为 mpl_toolkits 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18596410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:12:02  来源:igfitidea点击:

ImportError: No module named mpl_toolkits with maptlotlib 1.3.0 and py2exe

pythonmatplotlibpy2exepython-import

提问by RuiDC

I can't figure out how to be able to package this via py2exe now:

我现在不知道如何通过 py2exe 打包:

I am running the command:

我正在运行命令:

python setup2.py py2exe

via python 2.7.5 and matplotlib 1.3.0 and py2exe 0.6.9 and 0.6.10dev

通过 python 2.7.5 和 matplotlib 1.3.0 和 py2exe 0.6.9 和 0.6.10dev

This worked with matplotlib 1.2.x

这适用于 matplotlib 1.2.x

I have read http://www.py2exe.org/index.cgi/ExeWithEggsand tried to implement the suggestions for handling the mpl_toolkits since it's having become a namespace package.

我已阅读http://www.py2exe.org/index.cgi/ExeWithEggs并尝试实施处理 mpl_toolkits 的建议,因为它已成为命名空间包。

I'm trying to get an answer here too: http://matplotlib.1069221.n5.nabble.com/1-3-0-and-py2exe-regression-td41723.html

我也想在这里得到答案:http: //matplotlib.1069221.n5.nabble.com/1-3-0-and-py2exe-regression-td41723.html

Adding an empty __init__.pyto mpl_toolkits makes it work, but this is only a workaround to the problem.

将空添加__init__.py到 mpl_toolkits 使其工作,但这只是解决问题的方法。

Can anyone suggest what I need to make py2exe work with mpl_toolkits.axes_grid1 in matplotlib 1.3.0 ?:

任何人都可以建议我需要什么才能使 py2exe 与 matplotlib 1.3.0 中的 mpl_toolkits.axes_grid1 一起工作?:



test_mpl.py is:

test_mpl.py 是:

from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size

if __name__ == '__main__':
    print make_axes_locatable, axes_size


setup2.py is:

setup2.py 是:

import py2exe
import distutils.sysconfig
from distutils.core import setup

# attempts to get it to work
import modulefinder
import matplotlib
import mpl_toolkits.axes_grid1
__import__('pkg_resources').declare_namespace("mpl_toolkits")
__import__('pkg_resources').declare_namespace("mpl_toolkits.axes_grid1")
modulefinder.AddPackagePath("mpl_toolkits", matplotlib.__path__[0])
modulefinder.AddPackagePath("mpl_toolkits.axes_grid1", mpl_toolkits.axes_grid1.__path__[0])

# end of attempts to get it to work

options={'py2exe': {'packages' : ['matplotlib', 'mpl_toolkits.axes_grid1', 'pylab', 'zmq'],
                    'includes': ['zmq', 'six'],
                    'excludes': ['_gdk', '_gtk', '_gtkagg', '_tkagg', 'PyQt4.uic.port_v3', 'Tkconstants', 'Tkinter', 'tcl'],
                    'dll_excludes': ['libgdk-win32-2.0-0.dll',
                                     'libgdk_pixbuf-2.0-0.dll',
                                     'libgobject-2.0-0.dll',
                                     'tcl85.dll',
                                     'tk85.dll'],
                    'skip_archive': True },}

setup(console=['test_mpl.py'], options=options)


output is:

输出是:

running py2exe
*** searching for required modules ***
Traceback (most recent call last):
  File "setup2.py", line 23, in <module>
    setup(console=['test_mpl.py'], options=options)
  File "C:\Python27\lib\distutils\core.py", line 152, in setup
    dist.run_commands()
  File "C:\Python27\lib\distutils\dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 243, in run
    self._run()
  File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 296, in _run
    self.find_needed_modules(mf, required_files, required_modules)
  File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 1308, in find_needed_modules
    mf.import_hook(f)
  File "C:\Python27\lib\site-packages\py2exe\mf.py", line 719, in import_hook
    return Base.import_hook(self,name,caller,fromlist,level)
  File "C:\Python27\lib\site-packages\py2exe\mf.py", line 136, in import_hook
    q, tail = self.find_head_package(parent, name)
  File "C:\Python27\lib\site-packages\py2exe\mf.py", line 204, in find_head_package
    raise ImportError, "No module named " + qname
ImportError: No module named mpl_toolkits

回答by John David Reaver

Most folders in the site-packages directory in a Python installation are packages (they have an __init__.py file). If there is no __init__.py file, then the package is called a namespace package. cx_Freeze has an option to indicate that mpl_toolkits is a namespace package, so the subpackages can be found.

Python 安装中 site-packages 目录中的大多数文件夹都是包(它们有一个 __init__.py 文件)。如果没有 __init__.py 文件,则该包称为命名空间包。cx_Freeze 有一个选项表明 mpl_toolkits 是一个命名空间包,因此可以找到子包。

回答by joelostblom

There is a quite simple workaround to this problem. Find the directory from which mpl_tools is imported and simply add an empty text file named __init__.pyin that directory. py2exe will now find and include this module without any special imports needed in the setup file.

这个问题有一个非常简单的解决方法。找到从中导入 mpl_tools 的目录,然后添加一个以__init__.py该目录命名的空文本文件。py2exe 现在将查找并包含此模块,而无需在安装文件中进行任何特殊导入。

You can find the mpl_tools directory by typing the following in a python console:

您可以通过在 python 控制台中键入以下内容来找到 mpl_tools 目录:

import importlib
importlib.import_module('mpl_toolkits').__path__

I found the solution here https://stackoverflow.com/a/11632115/2166823and it seems to apply to namespace packages in general.

我在这里找到了解决方案https://stackoverflow.com/a/11632115/2166823,它似乎适用于一般的命名空间包。

回答by Aerin

This problem happened to me after I update MacOS to Sierra from El Capitan.

在我从 El Capitan 将 MacOS 更新到 Sierra 后,这个问题发生在我身上。

sudo pip install -U matplotlib

solved my problem.

解决了我的问题。

This page https://github.com/JuliaPy/PyPlot.jl/issues/294might help you as well.

此页面https://github.com/JuliaPy/PyPlot.jl/issues/294也可能对您有所帮助。