diff --git a/server_waitress.py b/server_waitress.py index bac4c4d..42d4e86 100644 --- a/server_waitress.py +++ b/server_waitress.py @@ -45,9 +45,17 @@ import sys from pathlib import Path # 设置控制台编码为 UTF-8(解决 Windows 中文乱码问题) +# PyInstaller onefile 模式下 sys.stdout.buffer 可能不存在(无控制台或管道重定向), +# 此时跳过编码包装,避免 AttributeError 导致启动崩溃 import io -sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') -sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8') +try: + sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') +except (AttributeError, ValueError): + pass +try: + sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8') +except (AttributeError, ValueError): + pass # Add the project root and src directory to PYTHONPATH project_root = Path(__file__).parent.absolute()