(Ignore the previous post,
 and remind NEVER edit an email inside a browser! :)

Hi folks,

I needed to include in my onefile package the library matplotlib
(http://matplotlib.sourceforge.net/)
In order to accomplish this task was necessary to hack a bit.
here are roughly the steps that I did, I hope that can be useful for someone.

1) Create a basic .spec file
>> python Makespec.py --windowed --onefile --paths $paths --out
$destdir --icon $icon $script

You obtain something like
# Specfile
# ------------------------
a = Analysis([os.path.join(HOMEPATH,'support\\_mountzlib.py'),
              os.path.join(HOMEPATH,'support\\useUnicode.py'),
              <...>],
pathex=[<a lot of paths>]
pyz = PYZ(a.pure)
exe = EXE(TkPKG(),
          pyz,
          a.scripts+ [('v', '', 'OPTION')],
          a.binaries,
          name='<scriptname>'.exe',
          debug=False,
          strip=False,
          upx=False,
          console=False,
          icon='<path to icon>')

2) Well now edit your spec file and add this other directives,
   ( unpackMatdata.py is pasted at the end of the file )

# Specfile
# ------------------------
a = Analysis([os.path.join(HOMEPATH,'support\\_mountzlib.py'),
              os.path.join(HOMEPATH,'support\\useUnicode.py'),
              '<absolute path>\unpackMatdata.py',             # ADD+
              <...>],
pathex=[<a lot of paths>]
pyz = PYZ(a.pure)
import matplotlib                                                         #ADD+
matplotpkg = PKG( Tree(matplotlib._get_data_path()),name='mpl-data.pkg')  #ADD+
exe = EXE(TkPKG(),
          pyz,
          matplotpkg,                                                     #ADD+
          a.scripts+ [('v', '', 'OPTION')],
          a.binaries,
          name='<scriptname>'.exe',
          debug=False,
          strip=False,
          upx=False,
          console=False,
          icon='<path to icon>')

3)run Build.py Specfile


Fortunately there are few libraries around that uses their own
data, but I think that pyinstaller needs a better user-support
for this cases.

Fabrizio Milo

# [1] unpackMatdata.py
# -------------------------------------------------------
import carchive
import sys
import os

this = carchive.CArchive(sys.executable)
mp = this.openEmbedded('mpl-data.pkg')
targetdir = os.environ['_MEIPASS2']
targetdir = os.path.join(targetdir,'mpl-data')
os.mkdir(targetdir)
os.environ["MATPLOTLIBDATA"] = targetdir
os.putenv("MATPLOTLIBDATA", targetdir)
print "Extracting Matplotlib data ...",
for fnm in mp.contents():
    try:
        stuff = mp.extract(fnm)[1]
        outnm = os.path.join(targetdir, fnm)
        dirnm = os.path.dirname(outnm)
        if not os.path.exists(dirnm):
            os.makedirs(dirnm)
        open(outnm, 'wb').write(stuff)
    except Exception,mex:
        print mex
mp = None
print "ok"
# -------------------------------------------------------

_______________________________________________
PyInstaller mailing list
[email protected]
http://lists.hpcf.upr.edu/mailman/listinfo/pyinstaller

Reply via email to