記: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 就能夠正常執行了。(在個人系統上這樣作是成功的!)

相關文章
相關標籤/搜索