简体   繁体   English

python .exe无法正常工作

[英]python .exe not working properly

I downloaded pyinstaller in order to turn my python scripts into an executable program. 我下载了pyinstaller,以便将我的python脚本变成可执行程序。 I have a main script ((homepage.py) that, when ran, it would display a window with two buttons (button1 and button2). Each of the two buttons, if clicked, would run its corresponding python script (input1.py or input2.py) that displays a window where the user inputs data to be graphed in excel. 我有一个主脚本((homepage.py),当它运行时,它会显示一个带有两个按钮的窗口(button1和button2)。如果单击这两个按钮,它们中的每一个都会运行相应的python脚本(input1.py或input2.py)显示一个窗口,用户在excel中输入要绘制的数据。

My scripts work correctly when I run them in Spyder through Anaconda. 当我通过Anaconda在Spyder中运行它们时,我的脚本正常工作。 However, when I turn my main script (Homepage.py) into an executable program and run it, the main window with the two buttons appear, but when I click any of the two buttons, the corresponding window does not appear and the program closes. 但是,当我将主脚本(Homepage.py)转换为可执行程序并运行它时,会出现带有两个按钮的主窗口,但是当我单击这两个按钮中的任何一个时,相应的窗口不会出现并且程序关闭。 Does anyone know why that would happen? 有谁知道为什么会这样? Do I need to turn my input1.py and input2.py into .exe programs as well like I did with homepage.py? 我是否需要将input1.py和input2.py转换为.exe程序,就像我使用homepage.py一样?

In my scripts, when I need click any of the buttons, I would hide the main window and show the new window. 在我的脚本中,当我需要点击任何按钮时,我会隐藏主窗口并显示新窗口。 Would hiding and showing be what is causing my program to not function correctly even if it is working in Spyder? 隐藏和显示是什么导致我的程序无法正常运行,即使它在Spyder中工作?

main window (homapge.py) imports and class name, 主窗口(homapge.py)导入和类名,

from PyQt5 import QtCore, QtGui, QtWidgets
from input1 import Ui_input1
from input2 import Ui_input2

class Ui_homepage(object):

button1 (input1.py) imports and class name, button1(input1.py)导入和类名,

from PyQt5 import QtCore, QtGui, QtWidgets
import csv, os, subprocess, output1

class Ui_input1(object):

button2 (input2.py) imports and class name, button2(input2.py)导入和类名,

from PyQt5 import QtCore, QtGui, QtWidgets
import csv, os, subprocess, output2

class Ui_input2(object):

Also, inside both of input1.py and input2.py, I have a button function that returns to the main window (homepage.py) when clicked, and inside that function I import, 另外,在input1.py和input2.py中,我有一个按钮功能,单击时返回主窗口(homepage.py),并在我导入的函数内部,

from homepage import Ui_homepage

At the end of homepage.py, input1.py, and input2.py I have this import, 在homepage.py,input1.py和input2.py的末尾我有这个导入,

if __name__ == "__main__":
    import sys

Let me know if you need any more clarification or code. 如果您需要更多说明或代码,请与我们联系。

EDIT 1: 编辑1:

The code that I use in the main window (homepage.py) that calls button1, 我在主窗口(homepage.py)中使用的代码,它调用button1,

self.button1.clicked.connect(self.displayWindow1)

def displayWindow1(self):
    self.window = QtWidgets.QMainWindow()
    self.uiInput1 = Ui_input1()
    self.uiInput1.setupUi(self.window)
    self.homepage.hide()
    self.window.showMaximized()

The code that I use in the main window (homepage.py) that calls button2, 我在主窗口(homepage.py)中使用的代码,它调用button2,

self.button2.clicked.connect(self.displayWindow2)

def displayWindow1(self):
    self.window = QtWidgets.QMainWindow()
    self.uiInput2 = Ui_input1()
    self.uiInput2.setupUi(self.window)
    self.homepage.hide()
    self.window.showMaximized()

EDIT 2 : 编辑2

I tried running the program after I changed, 我改变之后尝试运行程序

from input1 import Ui_input1
from input2 import Ui_input2

to, 至,

import input1 
import input2

I got this error in the command prompt window after typing pyinstaller --onefile --windowed homepage.exe , 键入pyinstaller --onefile --windowed homepage.exe后,在命令提示符窗口中出现此错误,

25160 WARNING: lib not found: MSVCR90.dll dependency of c:\users\bj914e
\appdata\local\continuum\anaconda3\Library\bin\zlib.dll

and I got these errors in a warnhomepage.txt file, 我在warnhomepage.txt文件中收到了这些错误,

missing module named resource - imported by posix, C:\Users\bj914e\Desktop
\DAMS\DAMS_Version_4\homepage.py
missing module named posix - imported by os, C:\Users\bj914e\Desktop
\DAMS\DAMS_Version_4\homepage.py
missing module named _posixsubprocess - imported by subprocess, C:\Users
\bj914e\Desktop\DAMS\DAMS_Version_4\homepage.py
missing module named org - imported by pickle, C:\Users\bj914e\Desktop
\DAMS\DAMS_Version_4\homepage.py
missing module named readline - imported by cmd, code, pdb, C:\Users
\bj914e\Desktop\DAMS\DAMS_Version_4\homepage.py
excluded module named _frozen_importlib - imported by importlib, 
importlib.abc, C:\Users\bj914e\Desktop\DAMS\DAMS_Version_4\homepage.py
missing module named _frozen_importlib_external - imported by 
importlib._bootstrap, importlib, importlib.abc, C:\Users\bj914e\Desktop
\DAMS\DAMS_Version_4\homepage.py
missing module named _winreg - imported by platform, C:\Users\bj914e
\Desktop\DAMS\DAMS_Version_4\homepage.py
missing module named _scproxy - imported by urllib.request
missing module named java - imported by platform, C:\Users\bj914e\Desktop
\DAMS\DAMS_Version_4\homepage.py
missing module named 'java.lang' - imported by platform, C:\Users\bj914e
\Desktop\DAMS\DAMS_Version_4\homepage.py, xml.sax._exceptions
missing module named vms_lib - imported by platform, C:\Users\bj914e
\Desktop\DAMS\DAMS_Version_4\homepage.py
missing module named termios - imported by tty, C:\Users\bj914e\Desktop
\DAMS\DAMS_Version_4\homepage.py, getpass
missing module named grp - imported by shutil, tarfile, C:\Users\bj914e
\Desktop\DAMS\DAMS_Version_4\homepage.py
missing module named pwd - imported by posixpath, shutil, tarfile, 
http.server, webbrowser, C:\Users\bj914e\Desktop\DAMS\DAMS_Version_4
\homepage.py, netrc, getpass
missing module named _dummy_threading - imported by dummy_threading, 
C:\Users\bj914e\Desktop\DAMS\DAMS_Version_4\homepage.py
missing module named 'org.python' - imported by copy, C:\Users\bj914e
\Desktop\DAMS\DAMS_Version_4\homepage.py, xml.sax
missing module named cPickle - imported by xlsxwriter.compat_collections
missing module named cStringIO - imported by cPickle
missing module named copy_reg - imported by cPickle, cStringIO
missing module named StringIO - imported by xlsxwriter.compatibility

UPDATE: 更新:

I was able to fix my problem. 我能够解决我的问题。 It turned out to be a very simple solution. 事实证明这是一个非常简单的解决方案。 After I clicked one of the buttons the program closed, but a command prompt window appeared and disappeared within a millisecond that I did not notice before until it was mentioned to me in the solution below. 单击其中一个按钮后,程序关闭,但是命令提示窗口出现并在一毫秒内消失,我之前没有注意到,直到我在下面的解决方案中提到它。 The solution was moving all of the data files that input1.py and input2.py use into the dist file with the .exe program. 解决方案是使用.exe程序将input1.py和input2.py使用的所有数据文件移动到dist文件中。

PS: some recording programs are too slow to record the window that appears and disappears, so I tried with my smartphone (iPhone) camera and it was able to detect it. PS:有些录制程序太慢,无法记录出现和消失的窗口,所以我尝试使用我的智能手机(iPhone)相机,它能够检测到它。

I had a very similar problem, the issue was a missing module(s). 我有一个非常类似的问题,问题是缺少模块。 Your exe may use other modules that pyinstaller did not detect. 您的exe可能使用pyinstaller未检测到的其他模块。 For me, as soon as I imported Decimal module my exe worked like a charm! 对我来说,只要我导入十进制模块,我的exe工作就像一个魅力! When your homepage.exe closes/crashes, it tells you which module is missing. 当homepage.exe关闭/崩溃时,它会告诉您缺少哪个模块。 You will only have a millisecond to see it, I recorded my screen and slowed down the footage to see the error message. 你只有一毫秒才能看到它,我录制了我的屏幕并放慢了录像以查看错误信息。 Unconventional, perhaps, but worked for me! 也许是非传统的,但为我工作!

It looks like you've ran into this issue here (basically the same as you but they're using TKinter). 看起来你在这里遇到了这个问题 (基本上和你一样,但是他们使用的是TKinter)。

The suggestion ( link again ) was to import the entire file like a module into your homepage.py program. 建议( 再次链接 )是将整个文件像模块一样导入到homepage.py程序中。

So change: 所以改变:

from input1 import Ui_input1
from input2 import Ui_input2 

To: 至:

import input1 
import input2 

Let me know how it works out :-) 让我知道它是如何工作的 :-)

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

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