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

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

开发环境

  python 2.7.12 + Windows7python

注意事项

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

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

pyinstaller安装步骤

  一、配置pip镜像源。pip配置方法参考pip配置和安装第三方模块。若是已经配置,跳过。ui

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

C:\Users\Administrator>pip install pyinstaller Collecting pyinstaller Downloading http://pypi.doubanio.com/packages/3c/86/909a8c35c5471919b3854c01f43843d9b5aed0e9948b63e560010f7f3429/PyIns taller-3.3.1.tar.gz (3.5MB) 100% |████████████████████████████████| 3.5MB 112kB/s Requirement 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 ... done Successfully installed pyinstaller-3.3.1

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

C:\Users>where pyinstaller c:\Python27\Scripts\pyinstaller.exe

pyinstaller基本语法

  pyinstaller [options] scriptspa

  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使用实例

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

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

d:\Program Files\Notepad++>pyinstaller -F ccc.py 213 INFO: PyInstaller: 3.3.1
226 INFO: Python: 2.7.12
237 INFO: Platform: Windows-7-6.1.7601-SP1 ....... 8136 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30729, 4940) 10315 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully. 10341 INFO: Bootloader c:\python27\lib\site-packages\PyInstaller\bootloader\Windows-32bit\run.exe 10355 INFO: checking EXE 10369 INFO: Building EXE because out00-EXE.toc is non existent 10386 INFO: Building EXE from out00-EXE.toc 10401 INFO: Appending archive to EXE d:\Program Files\Notepad++\dist\ccc.exe 10432 INFO: Building EXE from out00-EXE.toc completed successfully.

FAQs

  一、若是Python脚本使用到了第三方库,如何打包?

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

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

  二、个人python脚本主要是命令行输出,可是程序执行完就退出没法查看相关信息,如何处理?

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

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

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

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

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

相关文章
相关标签/搜索