What is Python Wheels? What's the use of python.whl?

Jun 01, 2021 Article blog

Table of contents

Python .whl files (or wheels) are a rarely mentioned part of python but they are important to the installation process for python包 If you've already installed python包 with pip it's highly likely that wheels will make your installation faster and more efficient.

Wheels are an assembly of the Python ecosystem that helps make the installation of packages work properly. T hey allow for faster installation and a more stable package distribution process. In this tutorial, you'll learn more about what wheels are, what they offer, and how they appeal and make it easier to use Python

(Recommended tutorial: python tutorial)

Introduction to wheels

Before you learn how to package a project into wheels, it's helpful to understand what it's like to use them from a user's perspective.

You can start this experiment by installing a Python包 in your environment as usual. In this case, install uWSGI 2.0.x

$ python -m pip install 'uwsgi==2.0.*'
 2 Collecting uwsgi==2.0.*
 3   Downloading uwsgi-2.0.18.tar.gz (801 kB)
 4      |████████████████████████████████| 801 kB 1.1 MB/s
 5 Building wheels for collected packages: uwsgi
 6   Building wheel for uwsgi (setup.py) ... done
 7   Created wheel for uwsgi ... uWSGI-2.0.18-cp38-cp38-macosx_10_15_x86_64.whl
 8   Stored in directory: /private/var/folders/jc/8_hqsz0x1tdbp05 ...
 9 Successfully built uwsgi
10 Installing collected packages: uwsgi
11 Successfully installed uwsgi-2.0.18

To fully install uWSGI pip takes several different steps:

  1. On line 3, it downloads a TAR file called uwsgi-2.0.18.tar.gz which is compressed with gzip
  2. On line 6, it tarball and builds a .whl file by calling setup.py
  3. On line 7, it marks the wheel as uWSGI-2.0 . 18-cp38-cp38-macosx_10_15_x86_64.whl
  4. On line 10, it installs the actual package after the wheels have been built.

pip tar.gz tarball retrieved by pip is a source distribution package, or sdist not a wheel. In some ways, sdist is an antonym for wheels.

The source code release contains the source code. T his includes not only Python code, but also the source code of any extensions bound to the package (usually written in C or c++ For source distributions, the extension is compiled on the user side rather than on the developer side.

The source distribution also contains a metadata package that is located in a directory named .egg-info This metadata helps build and install packages, but users don't really need to do anything with it.

From a developer's point of view, a source distribution package is created when you run the following command:

$ python setup.py sdist

Now try installing a different package: chardet :

$ python -m pip install 'chardet==3.*'
 2 Collecting chardet
 3   Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
 4      |████████████████████████████████| 133 kB 1.5 MB/s
 5 Installing collected packages: chardet
 6 Successfully installed chardet-3.0.4

You can see an output that is significantly different from uWSGI installation.

Download a .whl file directly from PyPI when you install chardet T he wheel name is chardet-3.0.4-py2.py3-none-any whl follows a specific naming convention that you'll see later. From the user's point of view, and more importantly, when pip finds a compatible wheel on PyPI there is no build phase.

From a developer's point of view, the wheel is the result of running the following command:

$ python setup.py bdist_wheel

Why does uWSGI give you a source distribution while chardet provides a wheel? Y ou can find out why by looking at the pages of each item on PyPI and navigating to the download file area. This section shows you what pip actually sees on the PyPI index server:

  1. Due to the complexity of the project, uWSGI provides only one source distribution (uWSGI -2.0.18 .tar.gz).
  2. chardet provides both a roulette wheel and a source code release, but pip prefers the roulette wheel if it is compatible with your system. You'll see later how to determine compatibility.

Another example of compatibility checking for wheel installation is psycopg2 which provides a large number of wheels for Windows but does not provide any wheels for Linux or macOS clients. This means that, pip depending on your specific settings, pip-installed psycopg2 can get a wheel or source distribution.

To avoid these types of compatibility issues, some packages offer multiple wheels, each for a specific Python implementation and the underlying operating system.

So far, you've seen some obvious differences between wheels and sdist but more importantly, their impact on the installation process.

(Recommended micro-course: python3 basic micro-course)

Wheels are installed faster

Above, you see a comparison between getting a pre-built wheel installation and downloading a sdist installation. Wheels make end-to-end installation of Python包 faster for two reasons:

  1. In other cases, wheels are typically smaller than source distribution packages, which means they can move faster between networks.
  2. Installing directly from wheels the intermediate steps of building packages from the source distribution.

Almost chardet installing chardet requires only a fraction of the time it takes uWSGI H owever, this is an unfair comparison because chardet is a significantly smaller and simpler package. Using different commands, you can create a more direct comparison that shows how different the wheels are.

You can let pip ignore its tilt to the wheels via the -no-binary option:

$ time python -m pip install \
      --no-cache-dir \
      --force-reinstall \
      --no-binary=:all: \

This command calculates the installation time of the encryption package and tells pip use the source distribution package, even if a suitable wheel is available. Includes: all: Make rules applicable to cryptography and all of its dependencies.

On my machine, it takes about 32 seconds from start to finish. Not only does it take a long time to install, but building encryption also requires OpenSSL development header that can be used for Python

Cryptography can now be reinstalled, but this time make sure pip uses wheels from PyPI B ecause pip prefers wheels, this is similar to calling pip install completely without parameters. But in this case, you can make the intention explicit by requiring the wheel - pure binary:

This option takes just over 4 seconds, or only one-eighth of the time it takes for cryptography and its dependencies to use the source distribution.

Here's a look at Python Wheels