PyInstaller打包Python程序后运行闪退,新手求助
问题:PyInstaller打包exe后程序打开即闪退,源码运行正常
错误信息
Could not get source code. Error: Off when run file by exe
问题描述
直接在代码编辑器中运行源代码无报错,但使用PyInstaller将源代码打包成exe后,程序打开即立刻关闭。
源代码
from re import sub from decimal import Decimal import inflect import PySimpleGUI as sg def convert_to_numbers(money): money = money.replace(',', '') value = Decimal(sub(r'[^\d.]', '', money)) integer_part, decimal_part = int(value), int((value % 1) * 100) integer_in_words = p.number_to_words(integer_part).upper().replace(',','') decimal_in_words = p.number_to_words(decimal_part).upper().replace(',','') return f"SAY TOTAL: U.S. DOLLARS {integer_in_words} AND CENTS {decimal_in_words} ONLY." if decimal_part > 0 else f"SAY TOTAL: U.S. DOLLARS {integer_in_words}." layout = [ [sg.Button('CONVERT', button_color=('yellow', 'red'), size=(25, 2), font=('Helvetica', 13))], [sg.Text(' ' * 5)], [sg.Text('Paste the number into this box:', size=(25, 1), font=('Helvetica', 13)), sg.Input(key='Digital', size=(150, 3), font=('Helvetica', 13))], [sg.Text(' ' * 5)], [sg.Text('Values copy:', size=(25, 1), font=('Helvetica', 13)), sg.Input(key='Text', size=(150, 3), font=('Helvetica', 13))] ] window = sg.Window('CONVERT MONEY', layout, size=(800, 250), resizable=True, finalize=True) p = inflect.engine() while True: event, values = window.read() if event == sg.WIN_CLOSED: break if event == 'CONVERT' and values['Digital']: if not values['Digital'].replace('.', '').replace(',', '').isdigit(): sg.popup("Please enter a valid number!") else: result = convert_to_numbers(values['Digital']) if result: window['Text'].update(result) else: sg.popup("Please enter a valid number!") window.close()
解决方案
1. 先定位具体错误原因
程序闪退大概率是运行时抛出了异常但没显示,你可以通过两种方式查看错误:
- 打开命令提示符(CMD),把exe文件拖到窗口里运行,就能看到控制台输出的错误信息。
- 修改代码添加全局异常捕获,让错误弹窗显示:
from re import sub from decimal import Decimal import inflect import PySimpleGUI as sg import traceback def convert_to_numbers(money): money = money.replace(',', '') value = Decimal(sub(r'[^\d.]', '', money)) integer_part, decimal_part = int(value), int((value % 1) * 100) integer_in_words = p.number_to_words(integer_part).upper().replace(',','') decimal_in_words = p.number_to_words(decimal_part).upper().replace(',','') return f"SAY TOTAL: U.S. DOLLARS {integer_in_words} AND CENTS {decimal_in_words} ONLY." if decimal_part > 0 else f"SAY TOTAL: U.S. DOLLARS {integer_in_words}." layout = [ [sg.Button('CONVERT', button_color=('yellow', 'red'), size=(25, 2), font=('Helvetica', 13))], [sg.Text(' ' * 5)], [sg.Text('Paste the number into this box:', size=(25, 1), font=('Helvetica', 13)), sg.Input(key='Digital', size=(150, 3), font=('Helvetica', 13))], [sg.Text(' ' * 5)], [sg.Text('Values copy:', size=(25, 1), font=('Helvetica', 13)), sg.Input(key='Text', size=(150, 3), font=('Helvetica', 13))] ] try: window = sg.Window('CONVERT MONEY', layout, size=(800, 250), resizable=True, finalize=True) p = inflect.engine() while True: event, values = window.read() if event == sg.WIN_CLOSED: break if event == 'CONVERT' and values['Digital']: if not values['Digital'].replace('.', '').replace(',', '').isdigit(): sg.popup("Please enter a valid number!") else: result = convert_to_numbers(values['Digital']) if result: window['Text'].update(result) else: sg.popup("Please enter a valid number!") except Exception as e: sg.popup(f"错误详情:\n{str(e)}\n\n{traceback.format_exc()}") finally: window.close()
2. 处理inflect库的资源打包问题
你的代码用到了inflect库,它依赖内部的字典文件,PyInstaller默认不会自动打包这些资源,导致exe运行时找不到文件报错。
打包时用以下命令手动指定资源路径:
pyinstaller --onefile --add-data "C:\PythonXX\Lib\site-packages\inflect\*;inflect" 你的脚本文件名.py
替换C:\PythonXX为你的实际Python安装路径,你的脚本文件名.py为源代码文件名。
或者通过spec文件配置:
- 生成spec文件:
pyi-makespec 你的脚本文件名.py
- 打开生成的
.spec文件,修改datas字段:
datas = [('你的Python路径/Lib/site-packages/inflect', 'inflect')],
- 用spec文件打包:
pyinstaller 你的脚本文件名.spec
3. 其他排查点
- 若用
--onefile打包仍有问题,试试去掉该参数,生成文件夹形式的exe,看是否能正常运行。 - 更新所有依赖库到最新版本:
pip install --upgrade inflect pyinstaller pysimplegui
内容的提问来源于stack exchange,提问作者Bao Trung
相关产品推荐
相关产品推荐

