Python程序可以很好地使用cmd,当我生成.exe时,它不会出现';行不通
我正在cmd中运行我的python程序,它运行得很好。 我安装了pyinstaller并将其更新为最新版本,从中生成了一个.exe文件。 如果我运行.exe文件,它将不工作。 它打开了一个窗口,几秒钟后就关闭了,而没有运行程序。我已经录制了我的屏幕,以便在窗户关闭前拍摄一张照片。 我已经做了一个没有任何导入的简单程序来测试py安装程序是否工作。 如果我运行这个.exe文件,它就会工作 这是我的密码Python程序可以很好地使用cmd,当我生成.exe时,它不会出现';行不通,python,pyinstaller,Python,Pyinstaller,我正在cmd中运行我的python程序,它运行得很好。 我安装了pyinstaller并将其更新为最新版本,从中生成了一个.exe文件。 如果我运行.exe文件,它将不工作。 它打开了一个窗口,几秒钟后就关闭了,而没有运行程序。我已经录制了我的屏幕,以便在窗户关闭前拍摄一张照片。 我已经做了一个没有任何导入的简单程序来测试py安装程序是否工作。 如果我运行这个.exe文件,它就会工作 这是我的密码 #Server import socket import sys import time im
#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版本有一些bug。
现在我用的是1.6.8,它运行得很好
pip install pynput==1.6.8
就像你猜到的,有些东西是进口的。Pyinstaller可能不会将导入的库烘焙到exe中。也许可以为pyinstaller寻找一个参数来实现这一点?