Skip to main content
 首页 » 编程设计

python之bundle (只是--onefile)

2025年05月04日14daizhj

请阅读所有这篇文章并帮助我。

我想用 pyinstaller 创建 --onefile 可执行文件。

我在开发方面有这些:

  • Windows 8.1 x64
  • Qt 5.2.1
  • python 27
  • PyQt5.2.1(使用 Visual Studio 2012 构建)
  • Visual Studio 2012
  • PyInstaller 2.1
  • pyWin32

以及部署端 (VirtualBox) 中的这些(作为干净的 VM):

  • Windows 8 x64
  • vcredist_x64 2012

这是我要构建的简单 python 程序:

#main.py 
import sys 
from PyQt5.QtWidgets import QApplication, QPushButton 
 
app = QApplication(sys.argv) 
 
win = QPushButton("Hello World!") 
win.show() 
 
sys.exit(app.exec_()) 
#------------------------------------------------ 

好的,当我将其构建为 --onedir (pyinstaller main.py) 时,它在开发端和部署端都运行良好。

当我将其构建为 --onefile (pyinstaller -F main.py) 时,它在开发端运行良好,但在部署端不起作用。

并给出这个错误:

This application failed to start because it could not find or load the Qt platform plugin "windows".

Available platform plugins are: minimal, offscreen, windows.

Reinstalling the application may fix this problem.

是我的错还是这栋楼的问题是什么?

就此错误而言,它无法在临时文件夹的 _MEIxxxxx 文件夹中找到 qt5_plugins 文件夹。

或者,您认为问题出在 sys 模块?如果是,我该怎么办?

提前感谢回复

更新:

我应该说我在构建时有这个警告和错误:

1024 WARNING: No such file C:\Python27\msvcp90.dll 1024 WARNING: Assembly incomplete 1026 ERROR: Assembly amd64_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_none not found

更新2:

我将 msvcp90.dll、msvcm90.dll 手动添加到 c:\Python27,此警告和错误已解决。

更新 3:

qt.conf:

[Paths] 
Plugins = qt5_plugins 

主要规范:

# -*- mode: python -*- 
a = Analysis(['main.py'], 
             pathex=['D:\\hello2'], 
             hiddenimports=['sip', "PyQt5.QtCore", "PyQt5.QtGui", "PyQt5.QtWidgets"], 
             hookspath=None, 
             runtime_hooks=None) 
pyz = PYZ(a.pure) 
plugins = [("qt5_plugins/platforms/qwindows.dll", 
             "C:\\Qt\\Qt5.2.1\\5.2.1\\msvc2012_64\\plugins\\platforms\\qwindows.dll", "BINARY")] 
data = [ 
  ("qt.conf", "qt.conf", "DATA") 
] 
exe = EXE( 
    pyz, 
    a.scripts, 
    a.binaries + plugins, 
    a.zipfiles, 
    a.datas + data, 
    name='main.exe', 
    debug=False, 
    strip=None, 
    upx=True, 
    console=True 
) 

qt5_plugins 是用 pyinstaller 自动创建的,有平台插件。我认为它不需要手动添加为 extralib。

请您参考如下方法:

我刚刚第一次尝试使用 pyinstaller 并收到了丢失的“windows”消息。

在查看了许多“解决方案”并尝试了各种方法后,我终于通过将 C:\Python34\Lib\site-packages\PyQt4\plugins\platforms 中的 qwindows.dll 放到应用程序目录 (dist) 中解决了这个问题qt4_plugins\平台 (我手动创建了platforms目录,pyinstaller创建了qt4_plugins目录)

虽然不如创建 qt.conf 文件那么优雅,但它使应用程序正常运行。

我应该补充一点,我正在使用 Windows 7、python 3.4 和 PyQt4。