Kivy 1.9.1 Windows软件包.spec单个exe

时间:2016-02-07 11:57:23

标签: windows kivy

我想制作一个我的应用程序的Windows程序包,大多数文件都在.exe中。根据{{​​3}}我安装了setuptools 19.2。我在Kivy 1.9.1稳定。

根据Kivy - Create package on Windows上的说明,我获得了以下正常工作 .spec文件:

from kivy.deps import sdl2, glew
# -*- mode: python -*-

block_cipher = None


a = Analysis(['..\\foo\\main.py'],
             pathex=['path\\KO-exe'],
             binaries=None,
             datas=None,
             hiddenimports=['six','packaging','packaging.version','webbrowser'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='foo',
          debug=False,
          strip=False,
          upx=True,
          console=True , icon='..\\foo\\ko.ico')
coll = COLLECT(exe, Tree('..\\foo\\'),
               a.binaries,
               a.zipfiles,
               a.datas,
               *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins )],
               strip=False,
               upx=True,
               name='foo')

但是我最终得到了一个包含许多文件的文件夹,而不是我可以分发给其他人的文件夹。因此,通过添加命令--onefile并对.spec文件应用相同的步骤,我得到以下 NOT WORKING .spec:

# -*- mode: python -*-
from kivy.deps import sdl2, glew

block_cipher = None


a = Analysis(['..\\foo\\main.py'],
             pathex=['path\\KO-exe'],
             binaries=None,
             datas=None,
             hiddenimports=['six','packaging','packaging.version','webbrowser'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz, Tree('..\\foo\\'),
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins )],
          name='foo',
          debug=False,
          strip=False,
          upx=True,
          console=True , icon='..\\foo\\ko.ico')

错误: .exe仅在build文件夹中,而不在dist中,并且在运行时:Error loading Python DLL: C:\Users\user\AppData\Local\Temp\_MEI81162\python27.dll (error code 126)

问题

我一直试图制作工作.spec和单个.spec的各种组合(添加COLLECT,移动Tree('..\\foo\\')),但无济于事。

  • 如果要使用单一的.exe?
  • ,我应该更改什么?
  • 如何选择要排除的文件/文件夹? (我应该在Tree()使用什么语法?)
  • 除了data \ db \ database.db之外,我几乎想要.exe中的所有内容(在.exe中太慢)。我该怎么做?

编辑3

使用来自@tito的帮助工作.spec文件:

from kivy.deps import sdl2, glew
# -*- mode: python -*-
#import pdb
import sys

sys.path += ["..\\foo\\"]

block_cipher = pyi_crypto.PyiBlockCipher(key='1234567890')

#'six','packaging','packaging.version',
a = Analysis(['..\\foo\\main.py'],
             pathex=['D:\\path\\KO-exe'],
             binaries=None,
             datas=None,

             hiddenimports=[
             'webbrowser',
             '__init__',
             'data.__init__',
             'data.screens.__init__',
             'data.screens.dbmanager',
             'data.screens.db_kv.__init__',
             'data.screens.db_kv.backupsd',
             ],

             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)

#pdb.set_trace()

# exclusion list
from os.path import join
from fnmatch import fnmatch
exclusion_patterns = (
    join("kivy_install", "data", "images", "testpattern.png"),
    join("kivy_install", "data", "images", "image-loading.gif"),
    join("kivy_install", "data", "keyboards*"),
    join("kivy_install", "data", "settings_kivy.json"),
    join("kivy_install", "data", "logo*"),
    join("kivy_install", "data", "fonts", "DejaVuSans*"),
    join("kivy_install", "modules*"),
    join("Include*"),
    join("sdl2-config"),
    # Filter app directory
    join("personal*"),
    join("sign-apk*"),
    join(".idea*"),
)

def can_exclude(fn):
    for pat in exclusion_patterns:
        if fnmatch(fn, pat):
            return True

a.datas = [x for x in a.datas if not can_exclude(x[0])]
a.binaries = [x for x in a.binaries if not can_exclude(x[0])]
# Filter app directory
appfolder = [x for x in Tree('..\\foo\\', excludes=['*.py','*.pyc']) if not can_exclude(x[0])]  

#print(a.zipped_data)

#pdb.set_trace()

pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='foo',
          debug=True,
          strip=False,
          upx=True,
          console=True , icon='..\\foo\\ko.ico')
coll = COLLECT(exe, appfolder,
               a.binaries,
               a.zipfiles,
               a.datas,
               *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins )],
               strip=False,
               upx=True,
               name='foo')

我想从我的app文件夹中排除文件夹和文件,感谢tito,通过你我也想出了如何过滤Tree()

在让PyInstaller排除文件后,我会尝试在输出文件上运行NSIS。

1 个答案:

答案 0 :(得分:1)

Imo,我不确切知道你的应用程序在单文件模式下失败的原因。但是由于使用pyinstaller中的onefile模式(它会在每个临时目录中运行时解压缩文件)的含义,使用NSIS进行安装可能会更好。

要选择要排除的模块,您可以将它们放在excludes中的Analysis参数中:

excludes=["pickle", "csv"]

要排除文件,我通常在分析后/ EXE之前进行手动排除。这是一个例子:

# exclusion list
from fnmatch import fnmatch
exclusion_patterns = (
    join("kivy_install", "data", "images", "testpattern.png"),
    join("kivy_install", "data", "images", "image-loading.gif"),
    join("kivy_install", "data", "keyboards*"),
    join("kivy_install", "data", "settings_kivy.json"),
    join("kivy_install", "data", "logo*"),
    join("kivy_install", "data", "fonts", "DejaVuSans*"),
    join("kivy_install", "modules*"),
    join("Include*"),
    join("sdl2-config"),
)

def can_exclude(fn):
    for pat in exclusion_patterns:
        if fnmatch(fn, pat):
            return True
a.datas = [x for x in a.datas if not can_exclude(x[0])]
a.binaries = [x for x in a.binaries if not can_exclude(x[0])]