PyInstaller打包Python脚本为exe后无终端窗口显示打印内容求助
解决PyInstaller打包后无终端显示print输出的问题
我编写了一个调用API并使用print("...")展示结果的Python脚本,通过PyInstaller将其打包为exe文件,供其他部门的非技术同事使用。运行该exe文件时,没有窗口弹出,但从日志可以确认代码已执行,只是无法显示print输出的内容。我不想使用tkinter创建UI,仅需要一个能显示文本进度的简单终端,恳请提供帮助。
以下是我的代码:
#functions to call apis here def main(): print("Connecting to API A1... ") result_ev = api_a1() if result_ev == 'OK': print("Result : ", result_ev) else: print("Error please check logs") print("------------------------------------------------------------ ") print("Connecting to API A2... ") result_pa = api_a2() if result_pa == 'OK': print("Result : ", result_pa) else: print("Error please check logs") if __name__ == '__main__': main()
我在Anaconda终端执行的打包命令为:pyinstaller get_data_api.py --onefile --windowed
解决方案
移除
--windowed参数:这个参数会强制程序以无窗口的后台模式运行,导致终端输出无法显示。修改打包命令为:pyinstaller get_data_api.py --onefile打包完成后,运行exe文件会自动弹出终端窗口,所有
print的内容都会实时显示在窗口中。可选:自定义终端窗口:如果需要让终端更友好(比如设置窗口标题),可以在脚本开头添加几行代码(无需依赖tkinter),以Windows系统为例:
import ctypes # 设置终端窗口标题 ctypes.windll.kernel32.SetConsoleTitleW("API数据获取进度") #functions to call apis here # 后续原有代码保持不变这样打包后的exe运行时,终端窗口会显示设置的标题,方便同事识别。
内容的提问来源于stack exchange,提问作者python_dev_29
相关产品推荐
相关产品推荐

