windows下python打包成exe可执行文件
安装pyinstaller
pip install pyinstaller
准备好python文件
hello.py
import os
print ("Hello, exe running successfully!")
os.system("pause")
打包成exe文件
在hello.py所在文件夹下打开终端,输入:
pyinstaller -F hello.py
然后就可以生成hello.exe了
双击运行,结果如下:
常用参数:
-i 添加图标,后面紧跟图标路径,图标格式必须是.ico
-F 打包成一个exe文件
-w 使用窗口,无控制台
-c 使用控制台,无窗口
-D 创建一个目录,里面包含exe以及其他一些依赖性文件
pyinstaller -h 来查看参数
打包成带图标的exe文件
pyinstaller -F -i 1.ico hello.py
打包成带图标的exe文件时遇到报错:
TypeError:from_buffer() cannot return the address of the raw string within a bytes or unicode object
解决办法是:
pip install --upgrade cffi
再次打包,成功:
双击执行: