py文件封装exe
模块: pyinstaller
官网: https://pyinstaller.readthedocs.io/en/latest/
安装:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pyinstaller
开始:
封装的 py 文件: ComplaintMain
cmd - cd 到py文件目录 > ↓
pyinstaller -F -i爬虫.ico ComplaintMain.py
# -i爬虫.ico 可省略 这是图标
若报错: RecursionError: maximum recursion depth exceeded
修改 ****.spec 文件 添加(第二行添加)
import sys
sys.setrecursionlimit(5000)
再次运行
pyinstaller ComplaintMain.spec
到此就结束了, 因为设置-F 会在dist文件下 生成 exe文件
若需要设置程序结束不关闭窗口 ↓
① 代码末尾 写强制休眠
time.sleep(600)
② ↓
# 设置 程序结束不关闭窗口,但会出现另一个窗口
def main_run():
win = tkinter.Tk()
win.title("爬虫-投诉退款")
****还没有写的代码*****
win.mainloop()
# 必须要有 __main__
if __name__ == '__main__':
main_run()
-F,-onefile 产生单个的可执行文件
-D,--onedir 产生一个目录(包含多个文件)作为可执行程序
-a,--ascii 不包含 Unicode 字符集支持
-d,--debug 产生 debug 版本的可执行文件
-w,--windowed,--noconsolc 指定程序运行时不显示命令行窗口(仅对 Windows 有效)
-c,--nowindowed,--console 指定使用命令行窗口运行程序(仅对 Windows 有效)
-o DIR,--out=DIR 指定 spec 文件的生成目录。如果没有指定,则默认使用当前目录来生成 spec 文件
-p DIR,--path=DIR 设置 Python 导入模块的路径(和设置 PYTHONPATH 环境变量的作用相似)。也可使用路径分隔符(Windows 使用分号,Linux 使用冒号)来分隔多个路径
-n NAME,--name=NAME 指定项目(产生的 spec)名字。如果省略该选项,那么第一个脚本的主文件名将作为 spec 的名字
参考网站: http://c.biancheng.net/view/2690.html
ICO图标在线生成转换工具: http://www.msxindl.com/tools/ico/