导出单个 .exe 时,PyInstaller 卡在“Building PKG ...”上

DJa*_*ppo 6 python compilation pyinstaller kivy

我写了一个 kivy 程序,我想通过 pyinstaller 将它导出到单个 .exe 文件中。我设法导出到多个文件(标准选项),但是当我将 --onefile 选项添加到 pyinstaller 时,进程卡在一行上说:

    INFO: Building PKG (CArchive) out00-PKG.pkg
Run Code Online (Sandbox Code Playgroud)

有谁知道怎么解决?只是时间问题还是我在出口过程中遗漏了什么?

我的项目:

我正在使用 python 3.6.4、kivy 1.9.0 和 pyinstaller 3.3.1。main.py 和 main.kv 文件(我只使用了 2 个文件)都在同一个文件夹中,从现在开始我将其称为 \project_folder\。在同一个文件夹中还有一个名为 icon.ico 的图标。

我也在使用 UPX (upx394a),它被下载到一个名为 \upx_path\upx394a 的文件夹中。

首先,我修改了我的 main.py 文件:

import kivy
import sys
import os

...

def resourcePath():
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS)
    return os.path.join(os.path.abspath("."))

...

if __name__=='__main__':
    kivy.resources.resource_add_path(resourcePath())
    MainApp().run()
Run Code Online (Sandbox Code Playgroud)

对于导出,我运行了一个 Windows 提示;我移动到 \project_folder\ 然后导出:

    pyinstaller main.py --onefile --clean -y --windowed --icon=icon.ico 
    --name MyApp --upx-dir=\upx_path\upx394a --exclude-module _tkinter 
    --exclude-module Tkinter --exclude-module enchant --exclude-module twisted
Run Code Online (Sandbox Code Playgroud)

我在以下位置找到了此选项: Kivy:编译为单个可执行文件

以这种方式成功创建 .spec 文件后,我继续修改 .spec 文件以正确创建 .exe:

1.

from kivy.deps import sdl2, glew
Run Code Online (Sandbox Code Playgroud)
  1. 在“EXE(pyz”之后,我补充说:

    Tree('\...\percorso dove si trova il file main.py\'),

  2. 在“a.datas”之后,我在下一行添加:

    *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],

然后我保存 .spec 文件并从提示符运行:

python -m PyInstaller MyApp.spec
Run Code Online (Sandbox Code Playgroud)

在这里,在提示上的一些输出之后,是 pyinstaller 卡住的地方。我尝试等待一段时间,但没有任何反应。

** 我的代码:**

我在这里粘贴我正在使用的代码,希望它有帮助: 1. main.py

# python 3.6.4

from kivy.config import Config 
Config.set('input', 'mouse', 'mouse, multitouch_on_demand')
# set non resizable window, specify heigth and width
Config.set('graphics', 'resizable', False)
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '600')
Config.set('graphics', 'borderless', False)

import kivy
import sys
import os
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout

# la funzione definita di seguito serve per esportazione in .exe
def resourcePath():
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS)
    return os.path.join(os.path.abspath("."))

class RootWidget(FloatLayout):
    pass

class MainApp(App):
    def build(self):
        return RootWidget()

if __name__=="__main__":
    kivy.resources.resource_add_path(resourcePath()) # add this line
    MainApp().run()
Run Code Online (Sandbox Code Playgroud)
  1. 主文件

    # File name: main.kv
    #:kivy 1.9.0
    
    #:set logo_image 'logo_1.png'
    <CreditLabel@Label>:    # custom class for credits window labels
    size_hint: [.4, .1]
    color: 1, 1, 1, 1
    
    <RootWidget>:
    TabbedPanel:
        do_default_tab: False
        tab_width: self.parent.width/5
        background_color: 0, 0, 0, 1
    
        TabbedPanelItem:
            text: 'Benvenuto!'
            color: 1, 0.5, 0, 1
            FloatLayout:
                Label:
                    size_hint: .4, .25
                    pos_hint: {'center_x': 0.5, 'center_y': 0.7}
                    text: 'Benvenuto in MyBaku!'
                    font_size: 40
                    color: 1, 0.5, 0, 1
    
                Label:
                    size_hint: .6, .25
                    pos_hint: {'center_x': 0.5, 'center_y': 0.55}
                    text: 'Bentornato Gianpietro! Prenditi il tuo tempo per visualizzare le tue statistiche.'
                    font_size: 18
                    color: 1, 1, 1, 1
                Label:
                    canvas:
                        Rectangle:
                            size: 80, 80
                            pos: self.right - (self.width * 0.15), self.top * 0.8 
                            source: logo_image
    
        TabbedPanelItem:
            text: 'Questa notte...'
            color: 1, 0.5, 0, 1
            FloatLayout:
    
        TabbedPanelItem:
            text: 'Statistiche globali'
            color: 1, 0.5, 0, 1
            FloatLayout:
    
        TabbedPanelItem:
            text: 'Credits'
            color: 1, 0.5, 0, 1
            FloatLayout:
                Label:
                    canvas:
                        Rectangle:
                            #:set coefficient .3
                            size: self.width * coefficient, self.width * coefficient
                            pos: self.center_x - (self.width * coefficient)/2, self.top * 0.5 
                            source: logo_image
                CreditLabel:
                    text: 'Software developed by Giampo (dev 0.1)'
                    pos_hint: {'center_x': .5, 'center_y': .45}
                CreditLabel:
                    text: 'Written with Python 3.6.4 using kivy 1.9.0'
                    pos_hint: {'center_x': .5, 'center_y': .40}
                CreditLabel:
                    text: 'Trento (Italy) - march 2018'
                    pos_hint: {'center_x': .5, 'center_y': .35}
    
    Run Code Online (Sandbox Code Playgroud)
  2. 附上卡住提示 提示问题截图

Ste*_*eri 9

尝试删除现有的 build 和 dist 目录,看看它是否解决了问题,这个解决方案对我有用,我在调用 pyinstaller 时也使用了完整路径,因为我有多个版本的 python。


小智 6

我有同样的问题,同样的“信息:”。我忘了以管理员身份运行cmd...我这样运行后,它能够完成转换。