利用PyInstaller将python程序.py转为.exe的方法详解,pyinstaller.py


前言

最近经常用到一个.py程序,但是每次在不同电脑上用,希望能把Python脚本发布为脱离Python平台运行的可执行程序,比如单个exe。PyInstalle满足要求。

PyInstaller本身并不属于Python包。在安装 pyinstaller之前需把python环境配置好。

安装pyinstaller

下载pyinstaller

解压到F:\PyInstaller-2.1(自选)(可以去官网下载最新版)

安装pywin32

pywin32-217.win32-py2.7.exe:点击下载

安装pyinstaller

1、进入cmd

cd F:\PyInstaller-2.1
python pyinstaller.py --console --onefile test.py

如果提示:

Usage: python pyinstaller.py [opts] [ …] | 
pyinstaller.py: error: Requires at least one scriptname file or exactly one .spec-file

则说明安装完成了。

测试打包

1、文件放在当前目录的pyinstaller-2.1文件夹里面

cd F:\PyInstaller-2.1
python pyinstaller.py --console --onefile test.py

2、命令运行成功后会生成一个test文件夹。在这个文件夹下面会有一个名为dist的文件夹,此文件夹下面有转换好的test.exe

3、上面编译出来的exe能够正常运行了,但带一个黑色的console,以下重新编译,加入–windowed –icon,取消–console

python pyinstaller.py -w --onefile --icon="my.ico" test.py

其中my.ico是你要给他加的自定义图标文件。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用python能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对帮客之家的支持。

评论关闭