如何修复Pyinstaller使其无法运行

时间:2019-05-30 16:31:05

标签: python exe pyinstaller python-3.7

我正在运行python 3.7.0 32位版本。我在将脚本转换为可执行文件时遇到问题。 Pyinstaller成功创建了build和dist文件夹,并且我有一个可执行文件,但是运行时它什么也没做。

我已经阅读了其他问题,并且使用set Path =%Path%; C:\ Windows \ System32 \ downlevel;而且我没有收到有关我的dll文件的任何警告,因此它们都很好。我也在python 3.6.2上尝试过,并遇到了同样的问题,我不确定发生了什么。

这是我尝试做的Hello World程序:

import tkinter as tk
class Go:
    def __init__(self,root):
        self.master = root
        self.frame = tk.Frame(self.master)
        self.testLabel = tk.Label(self.frame,text='hello world')
        self.testLabel.grid(row=0,column=0)
        self.frame.pack()

def main():
    root = tk.Tk()
    root.geometry('600x600')
    app = Go(root)

if __name__ == '__main__':
    main()

运行pyinstaller:

C:\Users\kiern\AppData\Local\Programs\Python\Python37-32\Scripts>set Path=%Path%;C:\Windows\System32\downlevel;

C:\Users\kiern\AppData\Local\Programs\Python\Python37- 
32\Scripts>pyinstaller --onefile test.py
73 INFO: PyInstaller: 3.4
75 INFO: Python: 3.7.0
76 INFO: Platform: Windows-10-10.0.17763-SP0
77 INFO: wrote C:\Users\kiern\AppData\Local\Programs\Python\Python37- 
32\Scripts\test.spec
83 INFO: UPX is not available.
87 INFO: Extending PYTHONPATH with paths
['C:\\Users\\kiern\\AppData\\Local\\Programs\\Python\\Python37- 
32\\Scripts',
'C:\\Users\\kiern\\AppData\\Local\\Programs\\Python\\Python37- 
32\\Scripts']
87 INFO: checking Analysis
91 INFO: Building Analysis because Analysis-00.toc is non existent
91 INFO: Initializing module dependency graph...
96 INFO: Initializing module graph hooks...
100 INFO: Analyzing base_library.zip ...
4348 INFO: running Analysis Analysis-00.toc
4381 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
required by c:\users\kiern\appdata\local\programs\python\python37-32\python.exe
4792 INFO: Caching module hooks...
4798 INFO: Analyzing 
C:\Users\kiern\AppData\Local\Programs\Python\Python37-32\Scripts\test.py
4978 INFO: Loading module hooks...
4979 INFO: Loading module hook "hook-encodings.py"...
5081 INFO: Loading module hook "hook-pydoc.py"...
5084 INFO: Loading module hook "hook-xml.py"...
5360 INFO: Loading module hook "hook-_tkinter.py"...
5624 INFO: checking Tree
5627 INFO: Building Tree because Tree-00.toc is non existent
5629 INFO: Building Tree Tree-00.toc
5751 INFO: checking Tree
5754 INFO: Building Tree because Tree-01.toc is non existent
5755 INFO: Building Tree Tree-01.toc
5798 INFO: Looking for ctypes DLLs
5798 INFO: Analyzing run-time hooks ...
5803 INFO: Including run-time hook 'pyi_rth__tkinter.py'
5814 INFO: Looking for dynamic libraries
6094 INFO: Looking for eggs
6095 INFO: Using Python library 
c:\users\kiern\appdata\local\programs\python\python37-32\python37.dll
6096 INFO: Found binding redirects:
[]
6163 INFO: Warnings written to 
C:\Users\kiern\AppData\Local\Programs\Python\Python37- 
32\Scripts\build\test\warn-test.txt
6264 INFO: Graph cross-reference written to 
C:\Users\kiern\AppData\Local\Programs\Python\Python37- 
32\Scripts\build\test\xref-test.html
6323 INFO: checking PYZ
6323 INFO: Building PYZ because PYZ-00.toc is non existent
6324 INFO: Building PYZ (ZlibArchive) 
C:\Users\kiern\AppData\Local\Programs\Python\Python37- 
32\Scripts\build\test\PYZ-00.pyz
6937 INFO: Building PYZ (ZlibArchive) 
C:\Users\kiern\AppData\Local\Programs\Python\Python37- 
32\Scripts\build\test\PYZ-00.pyz completed successfully.
6947 INFO: checking PKG
6947 INFO: Building PKG because PKG-00.toc is non existent
6951 INFO: Building PKG (CArchive) PKG-00.pkg
9744 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
9812 INFO: Bootloader 
c:\users\kiern\appdata\local\programs\python\python37-32\lib\site- 
packages\PyInstaller\bootloader\Windows-32bit\run.exe
9812 INFO: checking EXE
9817 INFO: Building EXE because EXE-00.toc is non existent
9820 INFO: Building EXE from EXE-00.toc
9822 INFO: Appending archive to EXE 
C:\Users\kiern\AppData\Local\Programs\Python\Python37- 
32\Scripts\dist\test.exe
9832 INFO: Building EXE from EXE-00.toc completed successfully.

C:\Users\kiern\AppData\Local\Programs\Python\Python37-32\Scripts>

我的预期目标是要有一个可执行程序,它实际上可以显示某些内容。有什么帮助吗?

编辑: 我只是试图做一个简单的程序,该程序执行主要操作,然后打印问候世界,并发生相同的问题,可执行文件将cmd调高约1秒钟,然后崩溃或停止,什么也没发生

1 个答案:

答案 0 :(得分:0)

您的代码很好,可以正常运行,但没有输出可看,因为您忘记添加mainloop()函数来显示TK窗口。

import tkinter as tk


class Go:
    def __init__(self, root):
        self.master = root
        self.frame = tk.Frame(self.master)
        self.testLabel = tk.Label(self.frame, text='hello world')
        self.testLabel.grid(row=0, column=0)
        self.frame.pack()
        # dont forget this
        self.master.mainloop()


def main():
    root = tk.Tk()
    root.geometry('600x600')
    Go(root)


if __name__ == '__main__':
    main()

然后,您只需使用pyinstaller -F script.py冻结应用即可。