【问题标题】:Can't build .exe using Pyinstaller无法使用 Pyinstaller 构建 .exe
【发布时间】:2020-09-16 14:16:46
【问题描述】:

我有 2 个文件,一个是 Main.py,另一个是 Autoe.ui,我希望两者都作为一个 .exe 我试过了

pyinstaller -w --add-data="Autoe.ui;." Main.py

这很好用,但它也会创建很多其他文件,我只想要一个 exe,所以我尝试了这个

pyinstaller.exe -w --onefile --add-data="Autoe.ui;." Main.py

这会创建一个单独的 .exe,但它不会运行,我收到一个弹出窗口说“无法执行脚本 Main”

【问题讨论】:

  • 您是否尝试单独构建它们?
  • 是的,Autoe.ui使用PyQt Designer然后通过python脚本集成,都在同一个文件夹中
  • 如果你已经通过 python 集成了这个文件,你不需要把它作为一个额外的文件包含进来。 PyInstaller 会自动包含它。
  • 要 100% 确定包含 ui,您可以使用 pyuic5 命令将其从 .ui 转换为 .py 文件。
  • @stilManiac 像我这样加载 ui 时进行集成,uic.loadUi("Autoe.ui", self) 现在我尝试仅使用 python 文件构建 .exe,同样的错误。这次它也不适用于pyinstaller -w Main.py。所以我想我确实需要添加 .ui 文件

标签: python python-3.x pyinstaller executable


【解决方案1】:

我在使用 Kivy 应用时遇到了类似的问题。在我的情况下,它是一个 kv 文件,而不是一个 ui。也许我所做的会对你有所帮助。 在带有 * .py 文件的文件夹中,我运行了命令:

pyinstaller --onefile -y --clean --windowed --icon=someicon.ico main.py

main.spec 文件出现在文件夹中。我已经编辑过了。

# -*- mode: python ; coding: utf-8 -*-
block_cipher = None

a = Analysis(["main.py"],
             pathex=["C:\\Users\\underground\\Desktop\\gdc"], #<<<<<<<path to folder of your app
             binaries=[]
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
             
a.datas += [('gdc.kv', 'C:\\Users\\underground\\Desktop\\gdc\\gdc.kv', 'DATA')]  #<<<< I added kv file to a.datas
excluded_binaries = ['VCRUNTIME140.dll']                        #<<<< I disabled this library because my app won't start on win10
a.binaries = TOC([x for x in a.binaries if x[0] not in excluded_binaries])

exe = EXE(pyz, Tree('C:\\Users\\underground\\Desktop\\gdc\\Data','Data'), 
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='main',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=False,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False , icon='ikona.ico')

当我编辑 spec 文件时,我再次运行了 pyintaller,但给出的是 spec 文件而不是 * .py 文件。

pyinstaller main.spec

...仅此而已。 您也可以尝试 GUI 版本 - auto-py-to-exe 而不是 pyinstaller。

【讨论】:

  • 两种方法都不行,我先打包成一个文件夹
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-20
  • 2022-12-22
  • 2023-01-26
  • 2013-11-08
  • 2016-02-12
  • 1970-01-01
  • 2013-06-10
相关资源
最近更新 更多