Matplotlib and cx_Freeze

Rob Gaddi rgaddi at technologyhighland.invalid
Mon Mar 9 18:58:18 EDT 2015


I'm having serious trouble getting cx_Freeze and matplotlib to agree 
where the mpl-data folder is on an Ubuntu 14.04 machine.

I'm configured with python3-matplotlib 1.3.1 having come from the Ubuntu 
repositories, and a (now slightly modified) cx_Freeze 4.3.4 from the 
cheese shop.

Originally I couldn't even get cx_Freeze to locate the mpl-data folder 
when freezing the package.  I got around that by hacking the matplotlib 
hook from:

def load_matplotlib(finder, module):
    """the matplotlib module requires data to be found in mpl-data in the
       same directory as the frozen executable so oblige it"""
    dir = os.path.join(module.path[0], "mpl-data")
    finder.IncludeFiles(dir, "mpl-data")

to:

def load_matplotlib(finder, module):
    """the matplotlib module requires data to be found in mpl-data in the
       same directory as the frozen executable so oblige it"""
    try:
        from matplotlib import get_data_path
        dir = get_data_path()
    except ImportError:
        dir = os.path.join(module.path[0], "mpl-data")
    finder.IncludeFiles(dir, "mpl-data")

So now the freeze copies mpl-data into build/exe.linux-x86_64-3.4/mpl-
data, but when I run my code:

    print('Matplotlib resource file:', matplotlib.matplotlib_fname())
    print('Matplotlib rcParams["datapath"]:', matplotlib.rcParams
["datapath"])
    print('Matplotlib data folder:', matplotlib.get_data_path())

I get:

  Matplotlib resource file: /etc/matplotlibrc
  Matplotlib rcParams["datapath"]: /usr/share/matplotlib/mpl-data
  Matplotlib data folder: /usr/share/matplotlib/mpl-data

So I copy in matplotlibrc, and set the datapath to hardcode the current 
program directory; ugly but workable.  And now I get:

  Matplotlib resource file: /home/rgaddi/Documents/Software/rsctest/build/
exe.linux-x86_64-3.4/matplotlibrc
  Matplotlib rcParams["datapath"]: /home/rgaddi/Documents/Software/
rsctest/build/exe.linux-x86_64-3.4/mpl-data
  Matplotlib data folder: /usr/share/matplotlib/mpl-data

It recognizes that I've set the datapath, but it still will only use the 
global system directory, thus defeating the point of the freeze.

I'm pretty much out of ideas at this point.  Anyone have any advice?  

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.



More information about the Python-list mailing list