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 用法
执行命令:
pyinstaller demo.py
在当前的目录下,将会生成两个文件夹:build和dist。
可选参数 | 格式举例 | 功能说明 |
---|---|---|
-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:pythonLibsite-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
,这样可以减少打包后的体积。