[英]Unable to create a functional executable with PyInstaller and PyQt
我已经尝试了很多时间,为Python项目创建可执行文件。 在这个项目中,我需要使用:
实际上,可执行文件已创建,但是当我尝试启动该可执行文件时,什么都没有发生,除了我的鼠标告诉我她已被占用。
因此,我试图通过编写一些基本程序来弄清楚问题出在哪里,这些程序可以浓缩我项目中需要的每个功能。 当我从python(3.5)启动此程序时,一切正常,但是当我执行pyinstaller生成的文件时,一切均不正常。 (如果需要,interface.py文件在此处,位于pastebin.com文件中 ,我认为它不太相关:它只是带有按钮的表单)
from PyQt4 import QtGui
from interface import Ui_Form
import serial
import subprocess
import sys, os
class win(QtGui.QWidget, Ui_Form):
"""docstring for win"""
def __init__(self):
super(win, self).__init__()
self.setupUi(self)
self.ser = serial.Serial("COM3", 9600)
self.pathBat = "cmd.bat"
def on_pushButton_clicked(self):
#if (self.ser.isOpen() and self.serAvr.isOpen()):
if True:
self.ser.write("start".encode())
p = subprocess.call(self.pathBat, creationflags=subprocess.CREATE_NEW_CONSOLE, **self.subprocess_args())
if p == 1:
self.writeLog("Works")
self.ser.write("stop".encode())
#self.writeLog(p.returncode)
def subprocess_args(include_stdout=True):
# The following is true only on Windows.
if hasattr(subprocess, 'STARTUPINFO'):
# On Windows, subprocess calls will pop up a command window by default
# when run from Pyinstaller with the ``--noconsole`` option. Avoid this
# distraction.
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
# Windows doesn't search the path by default. Pass it an environment so
# it will.
env = os.environ
else:
si = None
env = None
ret = {}
# On Windows, running this from the binary produced by Pyinstaller
# with the ``--noconsole`` option requires redirecting everything
# (stdin, stdout, stderr) to avoid an OSError exception
# "[Error 6] the handle is invalid."
ret.update({'stdin': subprocess.PIPE,
'stderr': subprocess.PIPE,
'startupinfo': si,
'env': env })
return ret
app = QtGui.QApplication(sys.argv)
v = win()
v.show()
sys.exit(app.exec_())
我补充说:“cmd.bat”,以在pyinstaller的.spec文件中的数据,而且功能subprocess_arg这里是为了避免与子问题(文档作为mentionned 这里 )
首先,我认为问题与子流程有关,我试图删除对该子流程的所有引用,但仍然无法正常工作。 串行相同。 此外,我尝试通过在.spec文件中设置debug = True
来调试可执行文件,但是如果我尝试从控制台执行该文件,则什么也没有发生,它在第一行仍然处于阻塞状态。
因此,如果有人可以提供帮助! 先感谢您 !
也许“冻结”应用程序找不到“ cmd.bat” !? 您可以通过将其替换为绝对路径来对其进行测试。
您的可执行文件会将“ cmd.bat”解压缩到python中可通过sys._MEIPASS
访问的临时文件夹中。 您应该使用os.path.join(sys._MEIPASS, "cmd.bat")
类的文件来查找文件!
如果您需要它: getattr(sys, 'frozen', False)
指示您的代码是否冻结(但仅适用于PyInstaller)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.