捆绑数据文件PyInstaller 2.1和MEIPASS错误--onefile [英] Bundling Data files with PyInstaller 2.1 and MEIPASS error --onefile
问题描述
这<一个href=\"http://stackoverflow.com/questions/7674790/bundling-data-files-with-pyinstaller-onefile/13790741#13790741\">question之前一直问,我似乎无法让我的PyInstaller正常工作。我在mainscript.py文件调用下面code:
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)
当我运行PY文件(IDLE内),我的应用程序完美和负荷运行所有的数据文件。然而,当我与PyInstaller 2.1捆绑它(一个文件的方法)我得到的exe文件后出现以下错误生成:
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!
**编辑**
下面正是我想做的事情。
Here is exactly what I want to do.
我的主要脚本有一个设置(进口)看起来像下面。基本上我希望能够有Matplotlib,底图,并在所有IT资源路径:
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'
我使用PyInstaller 2.1。我也使用Python 2.7.5(32位)。我的操作系统是Windows 8(64位)。我Matplotlib是1.3.0和底图是1.0.6。 wxPython的是2.8.12.1(统一code)。
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).
我去吩咐做:&GT; pyinstaller myscript.py
。这会产生我的.spec文件,我微微编辑。下面是我的编辑规范文件:
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')
我想这是一个文件的可执行所以数据文件应在可执行文件内包装。在其他pyinstallers我平时还没有与MEIPASS问题。但是,我需要使用2.1,因为Matplotlib和底图。如果有人能完美打造此exe - 可以请你告诉我,我需要调整?谢谢!
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*
如果任何人都可以找出如何用py2exe做到这一点 - 这将是巨大的。任何方式,我可以进入一个可执行文件,这将是值得的!
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!
推荐答案
我想我看到的问题。你不喂data_files到您的分析对象。
以下是我加入我的.spec文件我的数据文件:
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')
请注意我不使用COLLECT()都没有。
Notice I'm not using COLLECT() at all.
如果您结账时的2.1文档: PyInstaller规格文件操作你会发现收集()不用于--onefile模式
If you checkout the 2.1 documentation at: PyInstaller Spec File Operation you'll notice that COLLECT() isn't used for --onefile mode.
如果你看一下目录提取所指向sys._MEIPASS你可能会注意到,你的规范文件中数据文件根本不存在的。
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.
我希望这有助于。
这篇关于捆绑数据文件PyInstaller 2.1和MEIPASS错误--onefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!