Pysimplegui: [PyInstaller-Problem] Fehler beim Ausführen des Skripts beim Aufrufen des Unterprozesses

Erstellt am 17. Okt. 2019  ·  4Kommentare  ·  Quelle: PySimpleGUI/PySimpleGUI

Art der Probleme

Insekt

Betriebssystem

Windows 7

Python-Version

3.5

PySimpleGUI-Port und -Version

4.4.1

Ihr Erfahrungslevel in Monaten oder Jahren

Erfahrung in der Python-Programmierung
1

Insgesamt Programmiererfahrung
7

Haben Sie zuvor ein anderes Python-GUI-Framework (tkiner, Qt usw.) verwendet (ja/nein ist in Ordnung)?
Jawohl

Sie haben diese Schritte ausgeführt:

  • [ ja] Lesen Sie die Anweisungen zum Einreichen eines Problems
  • [ ja] Hauptdokumentation http://www.PySimpleGUI.org nach Ihrem Problem durchsucht
  • [ ] Durchsucht die Readme nach Ihrem spezifischen Port, wenn nicht PySimpleGUI (Qt, WX, Remi)
  • [ ] Nach Demoprogrammen gesucht, die Ihrem Ziel http://www.PySimpleGUI.com ähneln
  • [ ] Beachten Sie, dass es unter jedem Port auf GitHub auch Demoprogramme gibt
  • [ ja] Führen Sie Ihr Programm außerhalb Ihres Debuggers aus (von einer Befehlszeile aus)
  • [ ja] Probleme (offene und geschlossene) durchsucht, um zu sehen, ob sie bereits gemeldet wurden

Code oder Teilcode, der das Problem verursacht

Ich bin neu bei PySimpleGUI. Wenn ich den Modul-Unterprozess verwende und py in exe verpackt habe, stürzt es immer ab, wie im folgenden Screenshot. Aber wenn ich meine Py-Datei direkt in cmd ausführe, ist es in Ordnung. Also kann mir bitte jemand sagen, wie ich das beheben kann? Vielen Dank.

Absturz der Exe-Datei unter Windows 7, wenn auf OK geklickt wird:
enter image description here

SimpleDemoTestSubprocess.py:

import PySimpleGUI as sg
import subprocess


def runCommand(cmd, timeout=None):
    """ run shell command
    <strong i="38">@param</strong> cmd: command to execute
    <strong i="39">@param</strong> timeout: timeout for command execution
    <strong i="40">@return</strong>: (return code from command, command output)
    """

    prt('runCommand, cmd = ' + str(cmd))

    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output = ''

    prt('runCommand, communicate')
    out, err = p.communicate()
    prt('runCommand, wait')
    p.wait(timeout)

    prt(out)
    prt(err)

    return (out, err)


def prt(self, *args, sep=' ', end='\n', file=None):
    print()
    print(self, *args, sep=' ', end='\r\n', file=None)


# All the stuff inside your window.
layout = [
    [sg.Text('Some text on Row 1')]
    , [sg.Text('Enter something on Row 2'), sg.InputText()]
    , [sg.Button('Ok'), sg.Button('Cancel')]
    # , [sg.PopupScrolled('Hello From PySimpleGUI!', 'This is the shortest GUI program ever!')]
]

# Create the Window
window = sg.Window('Window Title', layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()
    if event in (None, 'Cancel'):  # if user closes window or clicks cancel
        break
    if event in (None, 'Ok'):  # if user closes window or clicks cancel
        runCommand("ls")
    print('You entered ', values[0])

window.close()

Paket py in exe:

# https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_EXE_Maker.py

import PySimpleGUI as sg
import subprocess
from shutil import copyfile
import shutil
import os

def prt(self, *args, sep=' ', end='\n', file=None):
    print()
    print(self, *args, sep=' ', end='\r\n', file=None)

def Launcher():
    sg.ChangeLookAndFeel('LightGreen')

    layout = [[sg.T('PyInstaller EXE Creator', font='Any 15')],
              [sg.T('Source Python File'), sg.In(key='_sourcefile_', size=(45, 1)),
               sg.FileBrowse(file_types=(("Python Files", "*.py"),))],
              [sg.T('Icon File'), sg.In(key='_iconfile_', size=(45, 1)),
               sg.FileBrowse(file_types=(("Icon Files", "*.ico"),))],
              [sg.Frame('Output', font='Any 15', layout=[[sg.Output(size=(65, 15), font='Courier 10')]])],
              [sg.ReadFormButton('Make EXE', bind_return_key=True),
               sg.SimpleButton('Quit', button_color=('white', 'firebrick3')), ]]

    window = sg.Window('PySimpleGUI EXE Maker',
                       auto_size_text=False,
                       auto_size_buttons=False,
                       default_element_size=(20, 1,),
                       text_justification='right')

    window.Layout(layout)

    # ---===--- Loop taking in user input --- #
    while True:
        (button, values) = window.Read()
        if button in ('Quit', None):
            break  # exit button clicked

        source_file = values['_sourcefile_']
        icon_file = values['_iconfile_']

        icon_option = '-i "{}"'.format(icon_file) if icon_file else ''
        source_path, source_filename = os.path.split(source_file)
        workpath_option = '--workpath "{}"'.format(source_path)
        dispath_option = '--distpath "{}"'.format(source_path)
        specpath_option = '--specpath "{}"'.format(source_path)
        folder_to_remove = os.path.join(source_path, source_filename[:-3])
        file_to_remove = os.path.join(source_path, source_filename[:-3] + '.spec')
        command_line = 'pyinstaller -wF "{}" {} {} {} {}'.format(source_file, icon_option, workpath_option,
                                                                 dispath_option, specpath_option)

        if button == 'Make EXE':
            try:
                prt('source_file: ' + str(source_file))
                prt('Making EXE... this will take a while.. the program has NOT locked up...')
                window.Refresh()

                prt('window.Refresh')
                window.Refresh()
                prt('Running command: {}'.format(command_line))
                runCommand(command_line)
                shutil.rmtree(folder_to_remove)
                os.remove(file_to_remove)
                prt('**** DONE ****')
            except Exception as e:
                # sg.PopupError('Something went wrong')
                prt("Launcher, Exception = " + e)


def runCommand(cmd, timeout=None):
    """ run shell command
    <strong i="6">@param</strong> cmd: command to execute
    <strong i="7">@param</strong> timeout: timeout for command execution
    <strong i="8">@return</strong>: (return code from command, command output)
    """

    prt('runCommand, cmd = ' + str(cmd))

    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output = ''

    prt('runCommand, communicate')
    out, err = p.communicate()
    prt('runCommand, wait')
    p.wait(timeout)

    prt(out)
    prt(err)

    return (out, err)

if __name__ == '__main__':
    Launcher()

Hilfreichster Kommentar

gelöst durch Änderung an diesem:

p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)

Alle 4 Kommentare

Dies ist ein Problem mit PyInstaller. Ich gehe davon aus, dass alles gut läuft, wenn Sie es nicht in eine EXE-Datei umwandeln. In der Vergangenheit konnte ich die Launcher-Demoprogramme, die Sie hier auf GitHub finden, aufgrund von PyInstaller-Problemen nicht in EXE-Dateien umwandeln.

Ich schlage vor, Stackoverflow oder anderswo auf das allgemeinere Problem von Subprocess-Aufrufen und PyInstaller zu überprüfen.

Wenn Sie den gesamten PySimpleGUI-Code entfernen und nur den Unterprozessaufruf aus Ihrer EXE-Datei aufrufen, hat das Probleme? Können Sie versuchen, das hart zu codieren und einen Test durchzuführen?

„Ich nehme an, es läuft alles gut, wenn Sie es nicht in eine EXE-Datei umwandeln.“
Ja, so ist es.

„Wenn Sie den gesamten PySimpleGUI-Code entfernen und nur den Unterprozessaufruf aus Ihrer EXE-Datei aufrufen, hat das Probleme?“
OK, ich habe alle PySimpleGUI-Codes entfernt und es stürzt immer noch ab. es hat also nichts mit PySimpleGUI zu tun :)

Hat PySimpleGUI also andere EXE-Packager unterstützt als Pyinstaller?

gelöst durch Änderung an diesem:

p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)

DANKE!!!!!!!!!

Ich versuche seit einiger Zeit dieses Problem zu lösen. Ich weiß es wirklich zu schätzen, dass Sie nicht nur graben und eine Lösung finden, sondern auch zurückkommen und sie posten. Sehr geschätzt!

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen

Verwandte Themen

yogesh-aggarwal picture yogesh-aggarwal  ·  3Kommentare

flowerbug picture flowerbug  ·  4Kommentare

MikeTheWatchGuy picture MikeTheWatchGuy  ·  5Kommentare

ECOM-Klaus picture ECOM-Klaus  ·  4Kommentare

ihouses picture ihouses  ·  6Kommentare