Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

python - Bundling Data files with PyInstaller 2.1 and MEIPASS error --onefile

This question has been asked before and I can't seem to get my PyInstaller to work correctly. I have invoked the following code in my mainscript.py file:

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

When I run the py file (within IDLE), my app runs perfectly and loads all of the data files. However, when I bundle it with PyInstaller 2.1 (one file method) I get the following error after the exe builds:

Traceback (most recent call last):
File "<string>", line 37, in <module>
WindowsError: [Error 3] The system cannot find the path   
specified: 'C:\Users\Me\AppData\Local\Temp\_MEI188722\eggs/*.*'

Does anyone have any idea where I went wrong? Thanks!

** EDIT **

Here is exactly what I want to do.

My main script has a setup (imports) that look like below. Essentially I want to be able to have Matplotlib, Basemap, and resource path all in it:

import os,sys
import matplotlib
matplotlib.use('WX')
import wx
from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from mpl_toolkits.basemap import Basemap
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import Calculate_Distance # A personal py file of mine

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

bmap=wx.Bitmap(resource_path('test_image.png'))

print 'hello'

I am using PyInstaller 2.1. I am also using Python 2.7.5 (32 bit). My OS is Windows 8 (64bit). My Matplotlib is 1.3.0 and Basemap is 1.0.6. Wxpython is 2.8.12.1 (Unicode).

I go to command and do: > pyinstaller myscript.py. This generates my .spec file which I slightly edit. Below is my edited spec file:

data_files = [('Calculate_Distance.py', 'C:\Users\Me\Documents\MyFolder\Calculate_Distance.py',
              'DATA'), ('test_image.png', 'C:\Users\Me\Documents\MyFolder\test_image.png',
              'DATA')]

includes = []
excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
            'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl',
            'Tkconstants', 'Tkinter']
packages = []
dll_excludes = []
dll_includes = []

a = Analysis(['myscript.py'],
             pathex=['C:\Users\Me\Documents\MyFolder','C:\Python27\Lib\site-packages\mpl_toolkits\basemap\*'],
             hiddenimports=[],
             hookspath=None,
             runtime_hooks=None)

pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts,
          a.binaries - dll_excludes + dll_includes + data_files,          
          name='MyApplication.exe',
          debug=False,
          strip=None,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=None,
               upx=True,
               name='MyApplication')  

I want this to be a one-file executable so the data files should be packed within the executable. On other pyinstallers I usually haven't had issues with the MEIPASS. However, I need to use 2.1 because of Matplotlib and Basemap. If someone can build this exe perfectly -- can you please tell me what I need to adjust? Thanks!

****EDIT****

If anyone can figure out how to do this with py2exe -- that would be great too. Any way that I can get this into a single executable would be worth it!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think I see the problem. You're not feeding data_files into your Analysis object. Here's how I add my data files in my .spec file:

a = Analysis(....)
a.datas += [('7z.dll', '7z.dll', 'DATA')]
a.datas += [('7z.exe', '7z.exe', 'DATA')]
a.datas += [('collection.xml', 'collection.xml', 'DATA')]
a.datas += [('License.html', 'License.html', 'DATA')]

pyz = PYZ(a.pure)

exe = EXE(pyz,
          a.scripts + [('O', '', 'OPTION')],
          a.binaries,
          a.zipfiles,
          a.datas,
          name=os.path.join('dist', 'blah.exe'),
          debug=False,
          strip=None,
          upx=False,
          console=True,
          icon=r'..NCM.ico')

Notice I'm not using COLLECT() at all.

If you checkout the 2.1 documentation at: PyInstaller Spec File Operation you'll notice that COLLECT() isn't used for --onefile mode.

If you look at the extracted directory pointed at by sys._MEIPASS you'll probably notice that with your spec file that the data file simply isn't there at all.

I hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...