美文网首页
python中py文件生成exe,命令行可运行但控制台出现闪退

python中py文件生成exe,命令行可运行但控制台出现闪退

作者: 丙吉 | 来源:发表于2023-06-26 14:58 被阅读0次

问题:打包一个最简单的py文件,生成exe,出现闪退的情况;

前期操作:

1。 安装pyinstaller 安装包
pip install pyinstaller 
2。 sayhello.py原始文件
def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
3。 把此py文件生成exe,用如下命令,可生成如下三个文件
pyinstaller -F sayhello.py
1687848419953.png
4。打开dist文件夹双击exe文件,出现闪退,但在命令行可运行,原来是程序运行后,控制台自动关闭引起的。
1687848516513.png 1687848613442.png

解决方法:

在原始文件中加入一句话,不让它那么快退出;

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    input('输入任意字符结束')
1687848756293.png
打开dist文件夹双击exe文件,结果如下:
1687849002849.png
其他闪退问题,可能是少了相关的安装包。

相关文章

网友评论

      本文标题:python中py文件生成exe,命令行可运行但控制台出现闪退

      本文链接:https://www.haomeiwen.com/subject/xzzwydtx.html