From fc70b9c958f5dbd0fd991aeb9a8a14cbf48420af Mon Sep 17 00:00:00 2001 From: hyb-oyqq <1512383570@qq.com> Date: Wed, 16 Jul 2025 14:36:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(source):=20=E5=A2=9E=E5=8A=A0=E5=AF=B9=20N?= =?UTF-8?q?uitka=20=E6=89=93=E5=8C=85=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 resource_path 函数以支持 Nuitka 打包环境 - 更新 .gitignore 文件 --- .gitignore | 3 ++- source/Main.py | 16 +++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 48fd17c..2eb05fb 100644 --- a/.gitignore +++ b/.gitignore @@ -170,4 +170,5 @@ cython_debug/ # PyPI configuration file .pypirc -nuitka-crash-report.xml \ No newline at end of file +nuitka-crash-report.xml +build.bat diff --git a/source/Main.py b/source/Main.py index 95a0144..d662a31 100644 --- a/source/Main.py +++ b/source/Main.py @@ -25,11 +25,17 @@ import sys import os def resource_path(relative_path): - """获取资源的绝对路径,适用于开发环境和PyInstaller打包环境""" - try: - # PyInstaller创建的临时文件夹 - base_path = sys._MEIPASS # type: ignore - except Exception: + """获取资源的绝对路径,适用于开发环境和Nuitka/PyInstaller打包环境""" + if getattr(sys, 'frozen', False): + # 如果是打包后的环境(Nuitka或PyInstaller) + if hasattr(sys, '_MEIPASS'): + # PyInstaller的临时路径 + base_path = sys._MEIPASS # type: ignore + else: + # Nuitka的路径或PyInstaller --onefile模式 + base_path = os.path.dirname(sys.executable) + else: + # 开发环境 base_path = os.path.dirname(os.path.abspath(__file__)) return os.path.join(base_path, relative_path)