首页 > 解决方案 > 无法使用 PyInstaller/ 创建工作 PyQt5 Python 可执行文件

问题描述

我正在尝试从我使用 PyQt5 编写的 Python 代码创建一个可执行文件,但无法让它工作。我一直在关注这个:https : //blog.aaronhktan.com/posts/2018/05/14/pyqt5-pyinstaller-executable example 在添加图像并将所有 PyQt5 的东西添加到隐藏的导入spec 文件,但似乎没有任何效果。

当它在我的桌面上创建时,我开发了代码,一切正常并且很好。图像显示出来,它直接从 dist 文件夹运行。但是当我通过电子邮件将 exe 发送到我的笔记本电脑并打开它时,我得到“无法执行脚本 abra_teleport”。我只是不确定它不喜欢什么。

编辑:我将代码更改为该代码,而不是 from PyQt5.QtWidgets import * 它具有 from PyQt5.QtWidgets import QLabel、QPushButton、QApplication、QMainWindow 但这并没有改变任何东西。

代码:

import sys
import os
import PyQt5
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class Abra(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title='I choose you! Abra!'
        self.left=100
        self.top=100
        self.width=320
        self.height=200
        self.window()

    def window(self):
        def resource_path(relative_path):
            if hasattr(sys, '_MEIPASS'):
                return os.path.join(sys._MEIPASS, relative_path)
            return os.path.join(os.path.abspath('.'), relative_path)
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.label=QLabel('',self)
        self.label.move(100,20)
        self.abra=QLabel(self)
        self.abra.setPixmap(QPixmap(resource_path('resources/sprites/abra.png')))
        self.abra.move(130, 90)
        self.button=QPushButton('Go Abra!',self)
        self.button.move(110,150)
        self.button.clicked.connect(self.on_click)
        self.show()


    def part1(self):
        def resource_path(relative_path):
            if hasattr(sys, '_MEIPASS'):
                return os.path.join(sys._MEIPASS, relative_path)
            return os.path.join(os.path.abspath('.'), relative_path)
        try:
            self.label.setText('')
            self.abra.setPixmap(QPixmap(resource_path('resources/sprites/abra.png')))
            self.button.setText('Go Abra!')
            self.button.clicked.connect(self.on_click)
        except:
            print('mmm')

    def on_click(self):
        try:
            self.label.setText('Abra used Teleport!')
            self.label.adjustSize()
            self.abra.setPixmap(QPixmap(''))
            self.button.setText('Try again!')
            self.button.clicked.connect(self.part1)
        except:
            print('nope')

if __name__=='__main__':
    app=QApplication(sys.argv)
    ex=Abra()
    ex.show()
    sys.exit(app.exec_())

规格文件:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['abra_teleport.py'],
             pathex=['C:\\Users\\marina\\Documents\\PokemonGUIProject\\Tests'],
             binaries=[],
             datas=[],
             hiddenimports=['PyQt5','PyQt5.QtWidgets','PyQt5.QtGui','PyQt5.QtCore'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)

a.datas += [('resources/sprites/abra.png','resources/sprites/abra.png','DATA')]

pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='abra_teleport',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=False )

标签: python-3.xpyqt5pyinstaller

解决方案


推荐阅读