Преглед изворни кода

fix(auth): 改进PyInstaller打包后的配置文件路径处理

- 修复了在PyInstaller单文件模式下配置文件路径不正确的问题
- 优先使用sys.argv[0]获取可执行文件路径,确保配置文件保存在正确位置
- 统一GUI和认证模块的配置文件路径,避免读写不一致
- 添加了新的测试脚本用于调试打包后的程序
- 重命名测试批处理文件以更好地反映其用途
mcbaiyun пре 2 месеци
родитељ
комит
104a623737
4 измењених фајлова са 18 додато и 3 уклоњено
  1. 12 1
      auth_session.py
  2. 5 2
      gui.py
  3. 0 0
      test_make_connections.bat
  4. 1 0
      test_rundist_debug.bat

+ 12 - 1
auth_session.py

@@ -39,7 +39,18 @@ def get_app_dir() -> Path:
     """
     try:
         if getattr(sys, "frozen", False):
-            return Path(sys.executable).resolve().parent
+            # When running as a PyInstaller bundle we want a stable location
+            # for runtime-writable files. For onefile mode PyInstaller
+            # extracts into a temporary folder and sets `sys.executable` to
+            # the extracted binary path. However `sys.argv[0]` normally
+            # contains the original path to the launched EXE (the file in
+            # the dist folder). Prefer using sys.argv[0] when available so
+            # writes go next to the original executable (e.g. dist\main.exe)
+            # rather than into the ephemeral temp extraction directory.
+            try:
+                return Path(sys.argv[0]).resolve().parent
+            except Exception:
+                return Path(sys.executable).resolve().parent
     except Exception:
         pass
 

+ 5 - 2
gui.py

@@ -14,11 +14,14 @@ import tkinter as tk
 from tkinter import messagebox
 from pathlib import Path
 
-from auth_session import load_config, get_base_url, login, set_sess_key
+from auth_session import load_config, get_base_url, login, set_sess_key, CONFIG_PATH as RUNTIME_CONFIG_PATH
 import protector_list
 
 ROOT = Path(__file__).resolve().parent
-CONFIG_PATH = ROOT / "config.json"
+# Use the runtime config path from auth_session so GUI saves to the same
+# location that the application reads at runtime (e.g. next to the exe when
+# packaged). During development this will typically be the working directory.
+CONFIG_PATH = RUNTIME_CONFIG_PATH
 
 
 def load_cfg():

+ 0 - 0
test.bat → test_make_connections.bat


+ 1 - 0
test_rundist_debug.bat

@@ -0,0 +1 @@
+.\dist\main.exe --debug