python编译为exe可执行文件
利用pyinstaller,可以将py文件打包为exe文件,从而不需要python环境和第三方依赖运行代码。
安装pyinstaller
pip install pyinstaller
或者
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyinstaller
打包hello world
写一个简单的hello world,文件名字就叫hello_world.py
print('hello world')
进入cmd
pyinstaller -F hello_world.py
命令输入之后会自动生成dist目录,在dist里面有我们打包好的文件
运行该文件
打包第三方库
pyinstaller -F 主程序 -p 次程序或者第三方库
第三方库如果不知道位置,可以cmd里面运行pip install 你想找的库。就可以输出位置,但注意后面的库位置会有库的版本,记得删掉。
例
import requests
print('hello world')
pyinstaller -F hello_world.py -p c:\users…\appdata\local\programs\python\python37-32\lib\site-packages\requests
添加exe的图标
pyinstaller -i 图标
版权声明:本文为cangyu51462原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。