You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyInstaller打包exe遇RuntimeError: input(): lost sys.stdin问题求助

解决PyInstaller打包exe后无法获取用户输入的问题

问题原因

出现RuntimeError: input(): lost sys.stdin错误,核心原因是打包后的exe没有关联标准输入流——大概率是你打包时用了-w/--windowed参数(这个参数会隐藏控制台窗口),导致input()找不到输入渠道。

快速解决方案

1. 重新打包(最推荐)

直接用不带窗口隐藏参数的PyInstaller命令打包:

pyinstaller 你的脚本文件名.py

如果之前打包时加了-w,删掉这个参数重新执行打包命令就行。打包完成后,运行dist文件夹里的exe,会弹出控制台窗口,就能正常输入内容了。

2. 代码层面修复(适合必须隐藏控制台的场景)

如果需要exe运行时不显示控制台,又要支持输入,可以给代码加一段修复逻辑:
首先安装依赖库:

pip install pywin32

然后修改你的代码:

import pandas as pd
import openpyxl
import sys
import os

# 修复stdin丢失问题
if sys.stdin is None:
    import win32console
    win32console.AllocConsole()
    sys.stdin = open('CONIN$', 'r')
    sys.stdout = open('CONOUT$', 'w')
    sys.stderr = open('CONOUT$', 'w')

downloaded_file = input("Enter Downloaded file name: ")
customer_file = input("Enter Customer file name: ")

df_portal = pd.read_excel(f"{downloaded_file}.xls")
df_customer_zyme = pd.read_excel(f"{customer_file}.xlsx")

这样打包时即使加了-w参数,运行exe也会临时弹出控制台用于输入,输入完成后可以继续执行后续逻辑。

内容的提问来源于stack exchange,提问作者Radhika

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 23:07:10