问题:打包一个最简单的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
![](https://img.haomeiwen.com/i7468301/55212d920626bb9c.png)
4。打开dist文件夹双击exe文件,出现闪退,但在命令行可运行,原来是程序运行后,控制台自动关闭引起的。
![](https://img.haomeiwen.com/i7468301/2bc0ca5ac81fa990.png)
![](https://img.haomeiwen.com/i7468301/26176185b5412896.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('输入任意字符结束')
![](https://img.haomeiwen.com/i7468301/c0da1cbe4dadcae8.png)
打开dist文件夹双击exe文件,结果如下:
![](https://img.haomeiwen.com/i7468301/d02a76afb81e0251.png)
网友评论