我已經嘗試了很多時間來爲Python項目創建可執行文件。在這個項目中,我需要使用:無法用PyInstaller和PyQt創建功能可執行文件
- PyQt的(4):我的GUI,
- PySerial:有一個Arduino溝通,
- 子過程:推出一些AVR事情.bat文件
事實上,可執行文件已創建,但是當我嘗試啓動它時,沒有任何反應,除了我的鼠標告訴我她已被佔用。
因此,我試圖通過編寫一些基本程序來了解哪裏可能存在問題,這些基本程序會濃縮我項目所需的每個功能。當我從python(3.5)啓動它時,所有東西都是有效的,但是當我執行由pyinstaller生成的文件時並沒有。 (該interface.py文件here, in a pastebin.com file,如果你願意,我認爲這不是很重要:它只有一個按鈕形式)
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」中的數據的.spec文件pyinstaller,並且功能subprocess_arg是在這裏,以避免與子問題
首先,我認爲這個問題是有聯繫的子過程(文檔here作爲mentionned),我試圖刪除它的所有引用,仍然沒有工作。相同的串行。此外,我試圖通過在.spec文件中設置debug = True
來調試可執行文件,但是如果我嘗試從控制檯執行該文件,則什麼都沒有發生,它在第一行保持阻塞狀態。
所以,如果任何人都可以幫忙!先謝謝你 !
你使用'console = False'嗎?這可能是您在命令行上看不到任何內容的原因。我最好的猜測是'界面'是問題的原因。確保它包含在構建中或創建一個沒有它的例子並嘗試「凍結」它。 – Repiklis
我使用'console = True'和'debug = True'。我試圖將'interface.py'添加到spec文件中包含的數據中,但仍然無法工作 – Wogle220