使用PyInstaller 2.1和MEIPASS错误--onefile捆绑数据文件 [英] Bundling Data files with PyInstaller 2.1 and MEIPASS error --onefile

查看:487
本文介绍了使用PyInstaller 2.1和MEIPASS错误--onefile捆绑数据文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题已被问到,我可以似乎让我的PyInstaller正常工作。我在mainscript.py文件中调用了以下代码:

  def resource_path(relative_path):
获取绝对路径资源,适用于dev和PyInstaller
try:
#PyInstaller创建一个临时文件夹,并存储路径在_MEIPASS
base_path = sys._MEIPASS
除外例外:
base_path = os.path.abspath(。)

返回os.path.join(base_path,relative_path)
pre>

当我运行py文件(在IDLE内)时,我的应用程序运行正常并加载所有的数据文件。但是,当我使用PyInstaller 2.1(一个文件方法)捆绑它时,在exe构建之后我收到以下错误:

 追溯(最近的电话最后):
文件< string>,第37行,< module>
WindowsError:[错误3]系统找不到路径
指定:'C:\\Users\\Me\\AppData\\Local\\Temp \\_MEI188722\\eggs /*.*'

有没有人有什么想法我错了?谢谢!



**编辑**



这是我要做的。



我的主脚本有一个设置(导入),如下所示。本质上,我想要能够使用Matplotlib,Basemap和资源路径:

  import os,sys 
import matplotlib
matplotlib.use('WX')
import matslot.bb b从mpl_toolkits.basemap导入底图
从matplotlib.figure import图
import matplotlib.pyplot as plt
import Calculate_Distance#个人py文件我的

def resource_path (relative_path):
获取资源的绝对路径,适用于开发人员和PyInstaller
try:
#PyInstaller创建一个临时文件夹,并将路径存储在_MEIPASS
base_path = sys._MEIPASS
除了异常:
base_path = os.path.abspath(。)

返回os.path.join(base_path,relative_path)

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

打印'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)。



我去命令行:> pyinstaller myscript.py 。这会生成我稍微编辑的.spec文件。以下是我编辑的规范文件:

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

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

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

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,可以告诉我需要调整什么?谢谢!



****编辑****



如果有人能弄清楚如何做到这一点与py2exe - 这将是伟大的。任何我可以将它变成一个可执行文件的方法都值得!

解决方案

我想我看到了这个问题。您没有将data_files提供给Analysis对象。
这是我如何在.spec文件中添加我的数据文件:

  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.zip文件,
a.datas,
name = os.path.join ('dist','blah.exe'),
debug = False,
strip = None,
upx = False,
console = True,
icon = r'.. \NCM.ico')

注意我没有使用COLLECT()全部。



如果您在以下位置签出2.1文档: PYIN你会注意到,COLLECT()不用于--onefile模式。



如果你看到提取的目录指向通过sys._MEIPASS,您可能会注意到,使用您的spec文件,数据文件根本就不在那里。



希望这有助于。


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!

解决方案

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.

这篇关于使用PyInstaller 2.1和MEIPASS错误--onefile捆绑数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆