16

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

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

This worked with matplotlib 1.2.x

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

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

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

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


test_mpl.py is:

from mpl_toolkits.axes_grid1 import make_axes_locatable, axes_size

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

setup2.py is:

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
RuiDC
  • 8,403
  • 7
  • 26
  • 21
  • you should really use pyinstaller, its the best out there – K DawG Sep 03 '13 at 16:21
  • We intend to move to cx_freeze given that unlike pyinstaller, it is py3k compatible, but py2exe is the incumbent in this space – RuiDC Sep 03 '13 at 17:50
  • pyinstaller has the same error, cx_Freeze unfortunately has problems with some tricks in numpy 1.7.x. – RuiDC Sep 04 '13 at 15:00
  • I struggle with a similar "No module named mpl_toolkits.axes_grid" while trying to import astro-delight package. No solution seems to be around. Have you resolved this? – Güray Hatipoğlu Jul 20 '23 at 16:01

6 Answers6

26

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__.py in that directory. py2exe will now find and include this module without any special imports needed in the setup file.

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

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

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

Community
  • 1
  • 1
joelostblom
  • 43,590
  • 17
  • 150
  • 159
  • i'd already added that in my original question: "Adding an empty __init__.py to mpl_toolkits makes it work, but this is only a workaround to the problem." – RuiDC Nov 08 '13 at 07:38
  • My bad, I read your question over at the matplotlib emailing list forum and then posted the workaround on there and here. Didn't notice that you added that line here. – joelostblom Nov 08 '13 at 15:19
  • 1
    Now the real question is why the `__init__.py` is left out in the first place. All I've got so far is "something something namespace packages". – tel Jan 05 '16 at 21:23
11

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

sudo pip install -U matplotlib

solved my problem.

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

aerin
  • 20,607
  • 28
  • 102
  • 140
3

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.

John David Reaver
  • 1,238
  • 1
  • 10
  • 17
  • thanks, that had been my preferred route, but as i'd mentioned in my comments, cx_Freeze had problems with numpy instead. – RuiDC Sep 25 '13 at 08:52
  • Ah, I forgot you wrote that. I came back to this question after a few days and didn't read it in detail again. Good luck! – John David Reaver Sep 25 '13 at 21:23
2

In my case I had this error " no module named 'mpl_toolkits.axes_grid' " and it was because of this line " from mpl_toolkits.axes_grid.inset_locator import inset_axes "

I realised that in previous versions the toolkit had a single namespace of "axes_grid". In more recent version (since svn r8226), the toolkit has divided into two separate namespace ("axes_grid1" and "axisartist").

So I changed to " from mpl_toolkits.axes_grid1.inset_locator import inset_axes " and problem solved!

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 25 '22 at 09:12
0

There is an module for it now

conda install basemap
mmann1123
  • 5,031
  • 7
  • 41
  • 49
0

Because maybe you forgot to install matplotlib. Please install first then import will work fine hopefully.

for Python just write this:

pip install matplotlib

for Python 3 write this:

pip3 install matplotlib

for Anaconda write this:

conda install matplotlib
MD. SHIFULLAH
  • 913
  • 10
  • 16