首页 > 编程 > Python > 正文

使用Pyinstaller转换.py文件为.exe可执行程序过程详解

2019-11-25 12:05:51
字体:
来源:转载
供稿:网友

 前言

pyinstaller能够在Windows、Linux等操作系统下将Python脚本打包成可直接运行程序。使Python脚本可以在没有安装Python的环境中直接运行,方便共享。

开发环境

python 2.7.12 + Windows7

注意事项

1、待转换的.py文件绝对路径最好不要包含中文字符。容易出现一些莫名其妙的问题。

2、python中需要有.py文件中用到的第三方库。否则在转换后的.exe文件中会出现不符合预期的结果。

pyinstaller安装步骤

1、配置pip镜像源。pip配置方法参考pip配置和安装第三方模块。如果已经配置,跳过。

2、打开cmd命令行窗口,输入pip install pyinstaller,安装pyinstaller库。

C:/Users/Administrator>pip install pyinstallerCollecting pyinstaller Downloading http://pypi.doubanio.com/packages/3c/86/909a8c35c5471919b3854c01f43843d9b5aed0e9948b63e560010f7f3429/PyInstaller-3.3.1.tar.gz (3.5MB)  100% || 3.5MB 112kB/sRequirement already satisfied: setuptools in c:/python27/lib/site-packages (from pyinstaller)Requirement already satisfied: pefile>=2017.8.1 in c:/python27/lib/site-packages (from pyinstaller)Requirement already satisfied: macholib>=1.8 in c:/python27/lib/site-packages (from pyinstaller)Requirement already satisfied: dis3 in c:/python27/lib/site-packages (from pyinstaller)Requirement already satisfied: future in c:/python27/lib/site-packages (from pyinstaller)Requirement already satisfied: altgraph>=0.15 in c:/python27/lib/site-packages (from macholib>=1.8->pyinstaller)Installing collected packages: pyinstaller Running setup.py install for pyinstaller ... doneSuccessfully installed pyinstaller-3.3.1

3、确认pyinstaller安装结果,位于c:/Python27/Scripts路径下。执行where pyinstaller查看

C:/Users>where pyinstallerc:/Python27/Scripts/pyinstaller.exe

pyinstaller基本语法

pyinstaller [options] script

options常用选项说明:

-F,-onefile: 表示生成单个可执行文件,常用。-w, -windowed, -noconsole:表示去掉控制台窗口,这在GUI界面时非常有用。不过如果是命令行程序的话那就把这个选项删除吧!-p 表示你自己自定义需要加载的类路径,一般情况下用不到-i 表示可执行文件的图标。注意:图片后缀必须是.ico-c,console,-nowindowed:使用控制台,无窗口(默认)-D,-onedir:创建一个目录,包含EXE文件,但会依赖很多文件(默认选项)

基本实例:pyinstaller -F myscript.py。

pyinstaller更多语法见官网说明: https://pyinstaller.readthedocs.io/en/stable/usage.html

pyinstaller原理简介

pyinstaller其实就是把python解释器和脚本打包成一个可执行文件,和编译成真正的机器码是完全两回事。所以打包不一定会提高运行效率,可能会降低运行效率,但是好处是在运行者机器上不用安装python和脚本所依赖的库。

输入指定的脚本后,首先pyinstaller会分析该脚本所依赖的其他依赖,然后进行查找、复制,把所有相关的依赖都收集起来并惊醒加密处理,包括python解释器,最后把这些文件放在一个目录下,或者打包到一个可执行文件。然后就可以直接运行所生成的可执行文件。

需要注意的是,使用pyinstaller打包生成的可执行文件,只能再和打包机器系统相同的环境下运行。32位python环境打包的程序可以运行在32/64位windows系统上。64位python环境打包的程序只能运行在64位windows系统上。所以如果想打包程序的话,建议使用32位python环境打包。

pyinstaller使用实例

1、确认待转换的.py文件可正确运行,不存在语法错误。如ccc.py

2、执行pyinstaller -F ${Python脚本名}完成文件转换。.exe文件生成的绝对路径会在倒数第二行显示,通常位于当前目录下dist所在目录下。转换后的.exe文件名与python文件名相同。如下图所示

d:/Program Files/Notepad++>pyinstaller -F ccc.pyINFO: PyInstaller: 3.3.1INFO: Python: 2.7.12INFO: Platform: Windows-7-6.1.7601-SP1.......INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 4940)INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.INFO: Bootloader c:/python27/lib/site-packages/PyInstaller/bootloader/Windows-32bit/run.exeINFO: checking EXEINFO: Building EXE because out00-EXE.toc is non existentINFO: Building EXE from out00-EXE.tocINFO: Appending archive to EXE d:/Program Files/Notepad++/dist/ccc.exeINFO: Building EXE from out00-EXE.toc completed successfully.

FAQs

1、如果Python脚本使用到了第三方库,如何打包?

方法一:将第三方库对应的包复制到待打包python脚本的同目录下,再执行打包命令。

方法二:pyinstaller.exe -F 路径/文件名.py 路径/文件名.py

2、我的python脚本主要是命令行输出,但是程序执行完就退出无法查看相关信息,如何处理?

在python脚本最后一行添加命令:os.system('pause') 或者 raw_input('Press enter any key to exit...')

3、 我想给我的打包后的执行程序换个图标,如何处理?

使用参数-i。如命令:pyinstaller -F -i tupian/qq.ico ccc.py。文件后缀名必须是.ico

4、程序运行出现CMD窗口,如何去除?

带上参数-w即可。pyinstaller.exe -F call_login.py -w (-w表示去掉控制台窗口显示)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表