zoukankan      html  css  js  c++  java
  • 使用Pyinstaller转换.py文件为.exe可执行程序

        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:UsersAdministrator>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:python27libsite-packages (from pyinstaller)
    Requirement already satisfied: pefile>=2017.8.1 in c:python27libsite-packages (from pyinstaller)
    Requirement already satisfied: macholib>=1.8 in c:python27libsite-packages (from pyinstaller)
    Requirement already satisfied: dis3 in c:python27libsite-packages (from pyinstaller)
    Requirement already satisfied: future in c:python27libsite-packages (from pyinstaller)
    Requirement already satisfied: altgraph>=0.15 in c:python27libsite-packages (from macholib>=1.8->pyinstaller)
    Installing collected packages: pyinstaller
      Running setup.py install for pyinstaller ... done
    Successfully installed pyinstaller-3.3.1

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

    C:Users>where pyinstaller
    c:Python27Scriptspyinstaller.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 FilesNotepad++>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:python27libsite-packagesPyInstallerootloaderWindows-32bit
    un.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 FilesNotepad++distccc.exe
    10432 INFO: 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 tupianqq.ico ccc.py。文件后缀名必须是.ico

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

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

  • 相关阅读:
    深入理解Elasticsearch写入过程 使用routing会导致docid不再全局唯一 Allocate策略 + routing参数
    force merge原理 + ES写入数据的过程
    Linux内核参数--time-wait回收--不建议设置
    elasticsearch迁移--利用腾讯云cos和nfs
    蓝鲸安装失败
    Apache Tomcat/8.5.51 secretRequired="true"
    自定义Nginx返回页面
    本地yum 源 iso文件 安装ansible
    elasticsearch字段属性值截断为32位
    rabbitmq-consul-apollo部署文档
  • 原文地址:https://www.cnblogs.com/linyfeng/p/8207149.html
Copyright © 2011-2022 走看看