【问题标题】:Python program works fine with cmd, when I make .exe it doens't workPython程序与cmd一起工作正常,当我制作.exe时它不起作用
【发布时间】:2020-10-28 20:06:38
【问题描述】:

我在 cmd 中运行我的 python 程序,它运行良好。 我安装了 pyinstaller 并将其更新到最新版本,从中制作了一个 .exe 文件。 如果我运行 .exe 文件是行不通的。 它正在打开一个窗口并在几秒钟内关闭而不运行程序。我已经记录了我的屏幕,以便在窗口关闭之前对其进行拍照。

我已经制作了一个没有任何导入的简单程序来测试 py 安装程序是否工作。 如果我运行那个 .exe 文件,它就可以工作。

这是我的代码;

#Server

import socket
import sys
import time
import pynput
from pynput.keyboard import Key, Controller

from datetime import datetime
sttime = datetime.now().strftime('%Y%m%d_%H:%M:%S - ')

keyboard = Controller()



print ('Opening program: TVV Sound Project - TCP to keyboard press V0.2 - Created by Schiettecatte Joris.')
print("")
print("")
print("")
with open('logging.txt', 'a') as logfile:
    logfile.write(sttime + 'Opening program: TVV Sound Project - TCP to keyboard press V0.2 - Created by Schiettecatte Joris.' + '\n')
    logfile.write('' + '\n')
    logfile.write('' + '\n')
    logfile.write('' + '\n')

try:
    with open('settings.txt', 'r') as settings_file:
        print("settings.txt has been read")
    with open('logging.txt', 'a') as logfile:
        logfile.write(sttime + 'settings.txt has been read' + '\n')

except FileNotFoundError:                                   #als settings.txt niet kan geopend worden zal dit volgende zaken doen.
    print ("settings.txt not found - creating file")
    with open('logging.txt', 'a') as logfile:
        logfile.write(sttime + 'settings.txt not found' + '\n')
   
else:
    with open('settings.txt', 'r') as settings_file:        
        IP_read = settings_file.read().split("\n")[0]
        IP = IP_read.split("#",1)[1]
        print("Settings - IP adresse: ", IP)
        with open('logging.txt', 'a') as logfile:
            logfile.write(sttime + 'IP adresse has been read: ' + IP + '\n')

    with open('settings.txt', 'r') as settings_file:        
        PORT_read = settings_file.read().split("\n")[1]
        PORT = PORT_read.split("#",1)[1]
        print("Settings - Port: ", PORT)
        with open('logging.txt', 'a') as logfile:
            logfile.write(sttime + 'PORT has been read: ' + PORT + '\n')
        PORT_INT = int(PORT)





    with open('settings.txt', 'r') as settings_file:        
        KEY1_read = settings_file.read().split("\n")[2]
        KEY1 = KEY1_read.split("#",1)[1]
        print("Settings - Key1: ", KEY1)
        with open('logging.txt', 'a') as logfile:
            logfile.write(sttime + 'Keyboard Key1 has been read: ' + KEY1 + '\n')

    print("")
    with open('logging.txt', 'a') as logfile:
        logfile.write('' + '\n')

 




s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = (IP, PORT_INT)
print("server will start on:",host)
print("")
with open('logging.txt', 'a') as logfile:
    logfile.write(sttime + 'server will start on: ' + IP + ':' + PORT + '\n')
    logfile.write('' + '\n')

s.bind(host)
print("Server done binding to host and port succesfully")
print("Server is waiting for incoming connections")
print("")
with open('logging.txt', 'a') as logfile:
    logfile.write(sttime + 'Server done binding to host and port succesfully' + '\n')
    logfile.write(sttime + 'Server is waiting for incoming connections' + '\n')
    logfile.write('' + '\n')


s.listen(1)
conn, addr=s.accept()
print(addr, "Has connected to the server and is now online.")
print("")

addr_ip = addr[0]
addr_port_int = addr[1]
addr_port = str(addr_port_int)
with open('logging.txt', 'a') as logfile:
    logfile.write(sttime + addr_ip + ':' + addr_port + ' Has connected to the server and is now online.' + '\n')
    logfile.write('' + '\n')


with conn:
    print('Connected by', addr)
    while True:
        try:
            incoming_message = conn.recv(1024)
            incoming_message_decode = incoming_message.decode()

            #print('niet decode:',incoming_message)
            #print(type(incoming_message))
            #print (len(incoming_message))

            #print('decoded:',incoming_message_decode)
            #print(type(incoming_message_decode))
            #print (len(incoming_message_decode))

            incoming_message_decode_trimmed = incoming_message_decode[0:8]
            print('Trimmed incoming decoded message: ',incoming_message_decode_trimmed)
            with open('logging.txt', 'a') as logfile:
                logfile.write(sttime + 'Trimmed incoming decoded message: ' + incoming_message_decode_trimmed + '\n')
                #print (len(incoming_message_decode_trimmed))


            if incoming_message_decode_trimmed == "string01":
                print("String meets the requirements")
                keyboard.press(KEY1)
                keyboard.release(KEY1)
                print("Key is pressed: ", KEY1)
                print("")
                with open('logging.txt', 'a') as logfile:
                    logfile.write(sttime + 'Key is pressed: ' + KEY1 + '\n')
                    logfile.write('' + '\n')

【问题讨论】:

  • 就像你猜到的那样,进口有些问题。 Pyinstaller 可能不会将导入的库烘焙到 exe 中。也许为执行此操作的 pyinstaller 寻找一个参数?

标签: python pyinstaller


【解决方案1】:

解决了! 看起来我使用的 pyinstaller 版本有一些错误。 现在我用的是 1.6.8,它工作正常。

pip install pynput==1.6.8

【讨论】:

    猜你喜欢
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 2014-09-01
    • 2021-04-08
    • 2017-12-25
    • 1970-01-01
    相关资源
    最近更新 更多