python3.7 打包(.exe)神器——pyinstaller 安装及用法

 

python打包工具都有哪些?主要有:py2exe、pyinstaller、cx_Freeze、nuitka等

 

工具名称 windows linux 是否支持单文件模式
bbfreeze yes yes no
py2exe yes no yes
pyinstaller yes yes yes
cx_Freeze yes yes no
py2app no no yes
nuitka yes yes yes

(上表来源于网络,未经考证)

 

本文介绍功能比较强大的pyinstaller

Github介绍:https://github.com/pyinstaller/pyinstaller

使用手册:https://pyinstaller.readthedocs.io/en/latest/#

 

一、 pyinstaller 介绍

以下摘取官网介绍(能力有限,英文可能翻译的不准确):

 

PyInstaller reads a Python script written by you. It analyzes your code to discover every other module and library your script needs in order to execute. Then it collects copies of all those files -- including the active Python interpreter! -- and puts them with your script in a single folder, or optionally in a single executable file.

PyInstaller is tested against Windows, Mac OS X, and GNU/Linux. However, it is not a cross-compiler: to make a Windows app you run PyInstaller in Windows; to make a GNU/Linux app you run it in GNU/Linux, etc. PyInstaller has been used successfully with AIX, Solaris, FreeBSD and OpenBSD, but is not tested against them as part of the continuous integration tests.

 

python程序的打包工具

支持Windows, Mac OS X, and GNU/Linux,但是不保证支持 AIX, Solaris, FreeBSD and OpenBSD

不能交叉编译,例如在win平台上打包的文件不能移植在Linux上运行

 

 

Main Advantages

  • Works out-of-the-box with any Python version 3.5-3.8.

  • Fully multi-platform, and uses the OS support to load the dynamic libraries, thus ensuring full compatibility.

  • Correctly bundles the major Python packages such as numpy, PyQt4, PyQt5, PySide, Django, wxPython, matplotlib and others out-of-the-box.

  • Compatible with many 3rd-party packages out-of-the-box. (All the required tricks to make external packages work are already integrated.)

  • Libraries like PyQt5, PyQt4, PySide, wxPython, matplotlib or Django are fully supported, without having to handle plugins or external data files manually.

  • Works with code signing on OS X.

  • Bundles MS Visual C++ DLLs on Windows.

 

主要优势:

开箱即用(双击exe文件即可运行),支持任何python3.5-3.8任何版本

支持多平台,利用操作系统下载动态库以保证兼容性

已经绑定了一些常用的安装包,如numpy, PyQt4, PyQt5, PySide, Django, wxPython, matplotlib等,并且兼容很多第三方安装包

 

Requirements and Tested Platforms

  • Python:
  • 3.5-3.8
  • tinyaes 1.0+ (only if using bytecode encryption). Instead of installing tinyaes, pip install pyinstaller[encryption] instead.
  • Windows (32bit/64bit):
  • PyInstaller should work on Windows 7 or newer, but we only officially support Windows 8+.
  • We don't support Python installed from the Windows store when not using virtual environments due to permission errors that can't easily be fixed.
  • GNU/Linux (32bit/64bit)
  • ldd: Console application to print the shared libraries required by each program or shared library. This typically can be found in the distribution-package glibc or libc-bin.
  • objdump: Console application to display information from object files. This typically can be found in the distribution-package binutils.
  • objcopy: Console application to copy and translate object files. This typically can be found in the distribution-package binutils, too.
  • Mac OS X (64bit):
  • Mac OS X 10.7 (Lion) or newer.

 

支持的python版本:3.5-3.8

支持的操作系统:win7 及以上 32bit/64bit、GNU/Linux (32bit/64bit)、Mac OS X 10.7 (Lion) 及以上

 

二、 pyinstaller 安装

pip install pyinstaller

 

三、pyinstaller 用法

 

假如我们要打包一个demo.py文件,基本过程是:打开cmd,并切换到demo.py文件所在的目录,注意路径中不要有中文
执行命令:pyinstaller demo.py
在当前的目录下,将会生成两个文件夹:build和dist。
dist里面就是所有可执行文件,点击demo.exe就能运行了。
 
pyinstaller指令的常见可选参数:
 
可选参数格式举例功能说明
-F pyinstaller -F demo.py 只在dist中生产一个demo.exe文件。
-D pyinstaller -D demo.py 默认选项,除了demo.exe外,还会在在dist中生成很多依赖文件,推荐使用。
-c pyinstaller -c demo.py 默认选项,只对windows有效,使用控制台,就像编译运行C程序后的黑色弹窗。
-w pyinstaller -w demo.py 只对windows有效,不使用控制台。
-p pyinstaller -p E:\python\Lib\site-packages demo.py 设置导入路径,一般用不到。
-i pyinstaller -i D:\file.icon demo.py 将file.icon设置为exe文件的图标,推荐一个icon网站:icon
 
上面的可选参数可以组合使用,比如pyinstaller -F -i D:\file.icon demo.py
 
能够from xxx import yyy就尽量不要import xxx,这样可以减少打包后的体积。
 
 
ps:我在安装时遇到找不到PyQt5.sip模块的问题
 
 

四、pyinstaller 耗时

我写了个小脚本打包后快200M了,而且非常慢……和C编译的速度差了几百万个数量级吧……也就是做做小程序用,大型的真心还是别折腾了



 

posted @ 2020-09-07 22:56  aby321  阅读(3448)  评论(0编辑  收藏  举报