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

PyInstaller无法使用父EXE中捆绑Python版本的问题求助

问题详情
  • 尝试从一个可执行文件(EXE)生成另一个EXE时,应用无法从AppData路径获取正确的Python环境
  • 之前遇到过通过AppData路径附加文件的问题,已通过先将文件复制到临时目录解决
  • 希望终端用户无需搭建完整Python环境即可运行简易应用,但该应用需要存储唯一登录信息以访问API
  • 在构建所用的虚拟环境中运行应用脚本时一切正常,能生成预期的最终EXE;但仅在从第一个EXE构建应用时出现问题
  • 额外异常:出现错误消息时应用会陷入循环,后续代码无法执行,且包裹PyInstaller代码的Try/Catch块未触发
调用PyInstaller的代码片段
save_path = 'C:/Users/' + getpass.getuser()

pyinstaller_opts = [
    tmp_folder + '/main.py',
    f"--add-data={tmp_folder + '/config.txt'}:.",
    f"--add-data={tmp_folder + '/avatar.png'}:.",
    '--onefile',
    '--noconsole',
    f'--icon={tmp_folder + "/deployments-reminder.ico"}',
    f'--distpath={save_path}',
    '--name',
    'deployments-reminder',

]

pyinstaller_opts = [
    'C:/Users/me/PycharmProjects/deployments-reminder-git/deployments-reminder.spec'
]

print('pyinstaller ' + ' '.join(pyinstaller_opts))
input("Press Enter to continue...")

try:
    print('okay')
    PyInstaller.__main__.run(pyinstaller_opts)
    print('oooookay?')
except Exception:
    print(traceback.format_exc())
    print("Unable to generate the EXE.")
    input("Press Enter to exit. ")
    sys.exit(1)
print('not okay')

print("")
print("Done!")

注:spec文件内容与上述生成的参数完全一致

可复现问题的示例

File 1(step_1.py)

import os
import PyInstaller
import sys
import shutil
import tempfile


tmp_folder = tempfile.gettempdir().replace(chr(92), '/')


def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)


def create_exe():
    pyinstaller_opts = [
        tmp_folder + '/step_2.py',
        '--name',
        'hello_world'
    ]

    PyInstaller.__main__.run(pyinstaller_opts)


if __name__ == '__main__':
    application = resource_path('step_2.py')
    shutil.copy(str(application), tmp_folder + '/step_2.py')
    create_exe()

    os.remove(tmp_folder + '/step_2.py')

    # compile with this
    # pyinstaller step_1.py -F --add-data=step_2.py:. -n test

File 2(step_2.py)

print('Hello, world!')

运行第一步生成的exe后,无法得到第二步的编译exe。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 07:42:15