记:PyInstaller打包一个最简单的kivy应用

OS: windows 10
python: 2.7.6 64bithtml

在kivy官网有篇介绍打包kivy应用的文章 传送门: Programming Guide » Create a package for Windows
我照葫芦画瓢,原样复制了代码,和步骤!打包成功!可是运行报错。python

Traceback (most recent call last):
  File "site-packages\PyInstaller\loader\rthooks\pyi_rth_pkgres.py", line 11, in <module>
  File "c:\users\wxg\appdata\local\temp\pip-build-px2be5\pyinstaller\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
  File "build\bdist.win-amd64\egg\pkg_resources\__init__.py", line 48, in <module>
  File "build\bdist.win-amd64\egg\pkg_resources\extern\__init__.py", line 60, in load_module
ImportError: The 'six' package is required; normally this is bundled with this package so if you get this warning, consult the packager of your distribution.
Failed to execute script pyi_rth_pkgres

six这个包。把ImportError: The 'six' package is required;做为关键字拿去搜索,最靠谱的一个答案是 stackoverflow 上的 Kivy - Create package on Windows
有人提到把 setuptools 的版本下降到 19.2 ,其实不必。
至少在个人系统上,我没有下降版本,也成功了。个人是 19.6.2 以下:windows

C:\Users\wxg\PycharmProjects\untitled\demo\helloworld>pip list
configparser (3.5.0)
docutils (0.12)
future (0.15.2)
Kivy (1.9.1)
Kivy-Garden (0.1.4)
kivy.deps.glew (0.1.4)
kivy.deps.sdl2 (0.1.12)
packaging (16.7)
pefile (2016.3.28)
pip (8.1.2)
Pygments (2.1.3)
PyInstaller (3.2)
pyparsing (2.1.4)
pypiwin32 (219)
requests (2.10.0)
setuptools (19.6.2)
six (1.10.0)
wheel (0.29.0)

完整的描述下吧:
(1)创建一个 helloKivy.py 文件,以下:app

import kivy

from kivy.app import App
from kivy.uix.label import Label

class MyApp(App):
    def build(self):
        return Label(text='Hello World')

if __name__ == '__main__':
    MyApp().run()

(2)安装一些必要的包,以下:ide

pip install pyinstaller six packaging configparser

(3)在 helloKivy.py 所在的目录下执行以下命令:ui

pyinstaller helloKivy.py

这会生成 helloKivy.spec 文件。
(4)参考官网的介绍,修改这个文件
在文件顶部添加以下代码:this

from kivy.deps import sdl2, glew

而后是修改 coll , 添加spa

Tree('C:\\Users\\wxg\\PycharmProjects\\untitled\\demo\\helloworld\\'),
*[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],

改完后是这个样子的:debug

coll = COLLECT(exe, Tree('C:\\Users\\wxg\\PycharmProjects\\untitled\\demo\\helloworld\\'),
               a.binaries,
               a.zipfiles,
               a.datas,
               *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
               strip=False,
               upx=True,
               name='helloKivy')

(5)而后并没结束。在个人系统中还要添加上下面这段:code

hiddenimports=['six','packaging','packaging.version','packaging.specifiers','configparser'],

这些都是咱们在第(2)步安装的。 'packaging.version' 和 'packaging.specifiers'是属于'packaging'的,必须写上。
完整的 helloKivy.spec 样貌就是这样子的:

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

block_cipher = None

a = Analysis(['helloKivy.py'],
             pathex=['C:\\Users\\wxg\\PycharmProjects\\untitled\\demo\\helloworld'],
             binaries=None,
             datas=None,
             hiddenimports=['six','packaging','packaging.version','packaging.specifiers','configparser'],
             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='helloKivy',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe, Tree('C:\\Users\\wxg\\PycharmProjects\\untitled\\demo\\helloworld\\'),
               a.binaries,
               a.zipfiles,
               a.datas,
               *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
               strip=False,
               upx=True,
               name='helloKivy')

(6)如今再来执行 pyinstaller helloKivy.spec 。注意是 helloKivy.spec ,若是你执行 helloKivy.pyhelloKivy.spec 就被重置了。

而后,生成的 helloKivy.exe 就能够正常执行了。(在个人系统上这样作是成功的!)

相关文章
相关标签/搜索