通常执行 python 程序要有相应的 Python 环境,但某些特定场景下,我们可能并不愿意这么麻烦的去配置这些环境(比如将写好的脚本发给客户进行操作),如果可以提前将程序打包成 Windows平台的 .exe
文件或者是Linux下的 .sh
脚本,那么使用起来就会方便很多,py2exe 和 PyInstaller 这两款工具都是干这么个事的,下面以 hello.py 脚本(代码内容如下)为例进行介绍。
age = input("How old are you?\n")
print("A: " + age)
PyInstaller
- 安装
pip install pyinstaller
- 使用
常见的用法有:
- 生成单个可执行文件:
pyinstaller -F hello.py
- 生成指定icon的可执行文件:
pyinstaller -i xxx.ico hello.py
py2exe
- 安装
pip install py2exe
- 使用
如上图,打包失败了,留意到这里说不支持 python3.6,果断放弃,有兴趣的可以自行降低到 python3.4 或 python3.5 进行尝试。