Stay Hungry,Stay Foolish!

python打包工具优缺点对比

python打包工具优缺点对比

PyInstaller

优点

 

  • 跨平台支持:能够在 Windows、macOS、Linux 等不同操作系统上使用,并且可以将 Python 脚本打包成对应平台的可执行文件。
  • 依赖管理:自动检测并打包 Python 脚本所依赖的库,减少了部署时的环境配置问题。
  • 简单易用:有简洁的命令行接口,只需一条命令就能完成打包操作,对新手友好。
  • 支持多种 Python 版本:兼容 Python 2.7 及 Python 3.x 等多个版本。

缺点

 

  • 打包文件体积大:由于会将 Python 解释器和所有依赖库一起打包,生成的可执行文件体积通常较大。
  • 某些依赖处理困难:对于一些特殊的依赖,如 C 扩展模块,可能需要额外的配置才能正确打包。
  • 打包速度较慢:尤其是在处理大型项目时,打包过程可能需要较长时间。

cx_Freeze

优点

 

  • 跨平台支持:与 PyInstaller 类似,支持在多个操作系统上打包 Python 程序。
  • 自定义程度高:可以通过编写 Python 脚本来配置打包过程,灵活性较强。
  • 支持多种 Python 版本:对不同版本的 Python 有较好的兼容性。

缺点

 

  • 文档不够完善:相比 PyInstaller,其官方文档不够详细,遇到问题时查找解决方案可能会比较困难。
  • 打包过程较复杂:需要编写 Python 脚本来配置打包参数,对于新手来说上手难度较大。
  • 依赖处理不够智能:在处理某些依赖时可能需要手动干预,不如 PyInstaller 自动化程度高。

Nuitka

优点

 

  • 执行速度快:Nuitka 会将 Python 代码编译成原生的 C 或 C++ 代码,生成的可执行文件执行速度通常比解释执行的 Python 代码要快。
  • 打包文件体积相对较小:由于是编译成原生代码,生成的可执行文件体积相对 PyInstaller 和 cx_Freeze 打包的文件要小一些。
  • 跨平台支持:支持在多个操作系统上使用。

缺点

 

  • 编译时间长:将 Python 代码编译成原生代码的过程通常需要较长时间,尤其是对于大型项目。
  • 兼容性问题:对于一些复杂的 Python 特性或第三方库,可能存在兼容性问题,需要进行额外的处理。
  • 学习成本高:使用 Nuitka 需要对编译原理和 C/C++ 有一定的了解,学习成本相对较高。

py2exe(仅适用于 Windows)

优点

 

  • 简单易用:提供了简单的命令行接口,方便将 Python 脚本打包成 Windows 可执行文件。
  • 依赖管理:能够自动处理 Python 脚本的依赖关系,简化部署过程。

缺点

 

  • 平台局限性:只能在 Windows 平台上使用,无法用于其他操作系统。
  • 不支持 Python 3.6 及以上版本:对较新的 Python 版本支持不足。
  • 社区活跃度低:由于该工具已经很久没有更新,社区活跃度较低,遇到问题时可能难以找到有效的解决方案。

 

综上所述,选择哪种打包工具取决于具体的需求。如果追求简单易用和跨平台支持,PyInstaller 是一个不错的选择;如果需要高度自定义打包过程,可以考虑 cx_Freeze;如果对执行速度有较高要求,Nuitka 可能更合适;而对于仅在 Windows 平台上部署的 Python 脚本,py2exe 可以作为一个简单的选择。

 

https://zhuanlan.zhihu.com/p/463597594

文档

PyInstaller文档Nuitka文档更加详细,条理性要更胜一筹。Nuitka文档中写的英语不是很标准,有语法错误,有时候会比较难看懂作者想要表达的意思 (作者本人不是以英语为母语的,所以可以理解吧)。

流行度

PyInstaller出来时间比Nuitka早,在国内流行程度也比较高 (国外不清楚)。通过百度搜索的结果数量可以知道目前PyInstaller被使用地更多。当然光从这一点上比较会不准确,大家可以从其他平台搜索结果上再比较下。

安全性

PyInstaller在打包时将py文件编译成pyc文件

,这个可以直接被反编译。你也可以通过--key命令在打包时进行加密,但也不安全。如果要做到更高程度的安全性,可以结合Cython来加密打包。Nuitka是直接将py文件编译成c语言级别的文件,安全性上面更胜一筹。

打包速度

对于简单的程序,两者的打包速度都是差不多的。不过碰到一个相对来说较大的库时,Nuitka会去编译这个库,导致它的打包速度非常非常非常地慢。当然,第三方库的代码是不需要加密的,所以我们可以通过--nofollow-imports命令不编译引入的库,通过--include-data-dir命令直接复制进来(当然你也可以后期手动复制)。PyInstaller默认是直接复制进来的,不会进行编译。

 

https://medium.com/@mpyatishev/python-on-the-go-pyinstaller-and-nuitka-d76e18650763

The conclusions from my experience with PyInstaller and Nuitka are as follows:

  • Both solutions work without special rituals, which is good. True, there were difficulties with the application’s static files, but this is a shortcoming of the application itself, not PyInstaller or Nuitka.
  • During the development stage, it is essential to make the right decision about where the data files and other static files will be stored and how they will be packaged or delivered to users.
  • Regarding build speed, PyInstaller significantly outperforms Nuitka. It is understandable — PyInstaller does not recompile the entire application and its dependencies but simply moves them to the target directory. Build time is important when building pipelines.
  • In terms of the size of the resulting directories/files, PyInstaller also outperforms Nuitka, although not significantly. This difference may not be as noticeable on larger projects.

 

https://sparxeng.com/blog/software/python-standalone-executable-generators-pyinstaller-nuitka-cx-freeze

Build Examples

The following command can be used to create a one-folder executable for the Python program defined in the Example Program section:

  • cmd> python -m PyInstaller main.py

 

This command is used to generate a one-folder executable name “main” (based on the name of the python module supplied at build-time) which is placed inside of the default distribution folder location of “dist”. Note that the names and locations of the files/folders can be modified if desired (see the official Options documentation page for more details).

File explorer window displaying main application folder.

The generated one-file folder can then be compressed to a zip folder and transferred to another machine. Users will then have to unzip the folder and launch the executable found within to use the application.

Now let’s use a slightly more complex build command:

  • cmd> python -m PyInstaller -F -w main.py

 

Here the “-F” argument is used to specify that a one-file executable is to be generated, and “-w” is used to indicate that a terminal window is not to be shown whenever the application starts (note that this is specific to Windows platforms). A one-file executable name “main” will be generated and placed in the default folder location of “dist”. This single application file can then be transferred to another machine and users will only have to execute it to use the application.

File explorer window displaying main application folder.

 

 

posted @ 2025-01-24 09:49  lightsong  阅读(450)  评论(0)    收藏  举报
千山鸟飞绝,万径人踪灭