使用 PyInstaller 2.1 和 MEIPASS 错误捆绑数据文件 --onefile [英] Bundling Data files with PyInstaller 2.1 and MEIPASS error --onefile
问题描述
这个问题之前有人问过,我可以似乎没有让我的 PyInstaller 正常工作.我在 mainscript.py 文件中调用了以下代码:
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!
** 编辑 **
这正是我想要做的.
我的主脚本有一个如下所示的设置(导入).基本上我希望能够在其中包含 Matplotlib、底图和资源路径:
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 (Unicode).
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).
我去命令并执行:<代码>>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')
我希望这是一个单文件可执行文件,因此数据文件应该打包在可执行文件中.在其他 pyinstaller 上,我通常没有遇到 MEIPASS 的问题.但是,由于 Matplotlib 和 Basemap,我需要使用 2.1.如果有人可以完美地构建这个 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!
****编辑****
如果有人能弄清楚如何用 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 输入到您的 Analysis 对象中.下面是我如何在我的 .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 Spec File Operation,您将请注意 COLLECT() 不用于 --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屋!