Python包类型(Wheel、Source等)详解

我们平时下载安装的python包统称为Distribution Package,而其中,又可分为Built Distribution(Wheel即为其中一种)和Source Distribution。

其中具体区别,请参见如下​​​​​​官网文档节选:

Distribution Package

A versioned archive file that contains Python packages, modules, and other resource files that are used to distribute a Release. The archive file is what an end-user will download from the internet and install.

Binary Distribution

A specific kind of Built Distribution that contains compiled extensions.

Built Distribution

A Distribution format containing files and metadata that only need to be moved to the correct location on the target system, to be installed. Wheel is such a format, whereas distutil's Source Distribution is not, in that it requires a build step before it can be installed. This format does not imply that Python files have to be precompiled (Wheel intentionally does not include compiled Python files).

为进一步理解Built Distribution和Binary Distribution的区别,给出如下两个示例:

1.使用pip安装numpy包

$ pip install numpy
Collecting numpy
  Downloading numpy-1.19.4-cp36-cp36m-manylinux2010_x86_64.whl (14.5 MB)
     |████████████████████████████████| 14.5 MB 15.1 MB/s 
Installing collected packages: numpy
Successfully installed numpy-1.19.4

可以发现,安装的numpy包的后缀名为.whl,即安装的为wheel文件,也即Built Distribution的一种,安装过程非常简单,且处理迅速。但这也给开发者带来了多平台兼容的困难。

2.使用pip安装uWSGI包

$ pip install uwsgi
Collecting uwsgi
  Downloading uWSGI-2.0.19.1.tar.gz (803 kB)
     |████████████████████████████████| 803 kB 274 kB/s 
Building wheels for collected packages: uwsgi
  Building wheel for uwsgi (setup.py) ... done
  Created wheel for uwsgi: filename=uWSGI-2.0.19.1-cp36-cp36m-linux_x86_64.whl size=597269 sha256=f2289db3db6d625f136f794be0319da477ad740eb28cd063971f8d335327c09c
  Stored in directory: /private/.cache/pip/wheels/3a/7a/c1/492f02e0cde1e39f8b75c79dc5ef3b7e2c93b02e3a7eaabe0c
Successfully built uwsgi
Installing collected packages: uwsgi
Successfully installed uwsgi-2.0.19.1

可以发现,安装的uWSGI包的后缀名为常见的压缩格式.tar.gz,这实际上就表示它为Source Distribution,即为经过编译的包发行版本。从上图流程中可以看到,Source Distribution相较于Built Distribution,多了在用户电脑上编译这一步,因此下载时往往需要附带编译依赖项,也就意味着更大的文件体积、更复杂的安装过程与更慢的处理速度。但由于未提前编译,所以更有利于多平台兼容。

综上所述

Wheel包的好处可概括为以下几点:

  1. 安装快;
  2. 一般比源发行版体积小很多。比方说matplotlib,它的wheel包最大只有11.6MB,而源码却有37.9MB。网络传输的压力显然是wheel小;
  3. 免除了setup.py的执行。setup.py是Python的disutils、setuptools编译机制所需要的文件,需要执行大量的编译、安装工作,如果没有wheel的存在,包的安装、维护都会很麻烦;
  4. 不需要编译器。pip在下载时帮你确定了版本、平台等信息;
  5. 使用wheel的话,pip自动为你生成.pyc文件,方便Python解释器的读取。
     

参考文献:

  • ​​​​​​​An Overview of Packaging for Python
  • 什么是wheel包,如何去用它? 

你可能感兴趣的:(经验分享,ubuntu,python,linux)