简体   繁体   English

pyinstaller转换后无法打开.exe

[英]Cannot open .exe after pyinstaller convert

Running my GUI application created in PyQt5 using any IDLE or the file.py is running perfectly, however, when I converted the .py to .exe using Pyinstaller I get an error every time I try to run the .exe file for some reason a small command window pops with an error and immediately disappear I screenshot the error before it closes. 使用任何IDLE或file.py运行在PyQt5中创建的GUI应用程序都可以正常运行,但是,当我使用Pyinstaller将.py转换为.exe时,由于某种原因,每次我尝试运行.exe文件时,都会收到错误消息a小命令窗口弹出错误,然后立即消失,我在错误关闭之前对其进行了截图。 Any Idea Thanks in advance 任何想法在此先感谢

Error image . 错误图片

I tried to execute different commands for pyinstaller but no luck. 我试图为pyinstaller执行不同的命令,但是没有运气。

<pyinstaller filename -F > 
<pyinstaller filename -onefile >
<pyinstaller filename >

It's an app over 900 lines and I cannot upload all of that but I think according to the error first lines. 这是一个超过900行的应用程序,我无法全部上传,但我认为是根据错误的第一行。 The error occurs so here are the lines of code. 发生错误,因此这里是代码行。 The problem is within importing modules I believe. 我认为问题出在导入模块之内。

from PyQt5.QtWidgets import QWidget
from PyQt5 import QtCore, QtGui, QtWidgets
from datetime import datetime
import time
import os, sys  #importing system modules

class Ui_MyTrophiesWindow(object):
    class save_txt_file(QWidget): 
     def GetSaveFile(self):
            path = QFileDialog.getSaveFileName(self,"Save MyTrophies.txt here", "MyTrophies", "*.txt")
            working_path = os.path.abspath(__file__)[0:-13]
            file = open(str(working_path) + "Txtpath.dat", "w+")
            for i in path:
                file.write(str(i))
            file.close()

It is very common that sometimes PyInstaller won't recognize all Qt5 dependencies especially QT core DLLs. 有时PyInstaller不能识别所有Qt5依赖项,尤其是QT核心DLL,这是很常见的。 A simple way is to just locate that file on your current Python path and feed it with add-data flag in PyInstaller. 一种简单的方法是仅在当前Python路径上找到该文件,并在PyInstaller中使用add-data标志add-data其提供。 Also, I suggest you not to use upx with PyQt as it may corrupt some DLLs: 另外,我建议您不要将upx与PyQt一起使用,因为它可能会损坏某些DLL:

pyinstaller --noupx -F --add-data "<Python_path>/Lib/site-packages/PyQt5/Qt/bin/Qt5Core.dll;./PyQt5/Qt/bin" script.py

To verify the answer suppose below example: 为了验证答案,假设以下示例:

import traceback
import os
import sys


def func():
   from PyQt5.QtWidgets import QWidget
   from PyQt5 import QtCore, QtGui, QtWidgets


try:
   if getattr(sys, 'frozen', False):
      print(sys._MEIPASS)
   func()
   print("Import OK!")
except Exception:
   traceback.print_exc()

input()

After you run the executable you would see the path for Pyinstaller (something like C:\\Users\\User\\AppData\\Local\\Temp\\_MEI90202 ), go to this path and search for the Qt5Core.dll and check if it is there. 运行可执行文件后,您将看到Pyinstaller的路径(类似C:\\Users\\User\\AppData\\Local\\Temp\\_MEI90202 ),转到此路径并搜索Qt5Core.dll并检查是否存在。

I had look your error and according to me, I think you need to (re)install Qt5core.dll module and to add it in the site-package/PyQt5/ init .py path. 我已经看过您的错误,并且据我说,我认为您需要(重新)安装Qt5core.dll模块并将其添加到site-package / PyQt5 / init .py路径中。 You can download the dll file here: 您可以在此处下载dll文件:

http://www.telecharger-dll.fr/dll-Qt5Core.dll.html http://www.telecharger-dll.fr/dll-Qt5Core.dll.html

Good evening 晚上好

Make sure that you have all the items of that app in the correct folder. 确保在正确的文件夹中拥有该应用程序的所有项目。 Let me put an example that happened to me days ago: 让我举一个几天前发生的事的例子:

I had an app with a folder named "options" that had 3 files (2 icons for my buttons ui and a .ini file). 我有一个带有名为“ options”的文件夹的应用程序,其中包含3个文件(我的按钮ui和.ini文件有2个图标)。 When I created the pyinstaller version of my program I assumed that it would somehow copy those files and pack them inside the --onefile file or into the dist folder (if not --onefile command was used). 当我创建程序的pyinstaller版本时,我假设它将以某种方式复制这些文件并将它们打包在--onefile文件中或打包到dist文件夹中(如果未使用--onefile命令)。 Nope, it didn't. 不,不是。 After scratching my head for hours, I just copied the options folder from my source files and pasted it next to my --onefile file (or inside the dist folder). 挠了几个小时之后,我只是从源文件中复制了options文件夹,并将其粘贴到我的--onefile文件旁边(或dist文件夹中)。

After that, my app works without issues. 之后,我的应用程序可以正常工作。 So... make sure it has all the files it needs in the correct folders. 因此,请确保它在正确的文件夹中具有所需的所有文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM