Understand the actual purpose of Python wheels
Python wheels are systems developed to facilitate faster and easier installation of different Python software. In other words, wheels are the new Python standard for the distribution and installation of Python packages. It is a more preferable approach to packaging as it lets you install mostly everything, that too without always needing a source distribution.
Through this tutorial, you will understand the actual purpose of wheels in Python and how you can use them.
Python Package Distribution: Previous Approaches
Earlier, Python provided two ways to install a package:
- From the source distribution(sdist):
It is the common method of installing packages from the source distribution using .zip/.tar.gz/other compressed files, usually using pip.
- egg files:
They are zip files of the respective package along with the metadata for that package. In simple words, they are a bundle of Python codes along with additional information.
Python wheels: Their Actual Purpose
As an improvement of the previous approaches, python wheels were introduced. A Python wheel is a pre-built package that you can install in one go as it only requires unzipping the compressed archive file. That is, there is no need to run many codes for installing the package. Thus, wheels not only saves time but also improves performance.
The zip archive comes with a specific filename followed by a .whl extension. It contains all the files necessary for the installation in such a way that it is very similar to the on-disk format.
Advantages of Wheels:
- There is no need to build software from the source, hence it saves time and improves performance.
- It provides improved security since you are not running arbitrary code from the internet but simply unzipping the files.
Structure of Python wheels
Wheels always come with the .whl extension and follow a specific naming convention that involves components separated by a dash(-).
These components include the name, version number and the platform triplet.
The platform triplet usually has 3 parts. The first part contains the versions of python that the package associates with, the implementation type, details about binary artifacts, DLL files present etc. The second part is the application binary interface which is usually none for wheels due to the absence of a binary interface. The third part contains installation specifications and requirements, platform details etc.
A sample format of the same can look something like this:
An Example of Python wheels
Let us now look into an example to understand better.
Consider the installation of any package as follows:
pip install Theano Collecting Theano Downloading Theano-1.0.5.tar.gz (2.8 MB) ---------------------------------------- 2.8/2.8 MB 1.9 MB/s eta 0:00:00 Preparing metadata (setup.py) ... done Requirement already satisfied: numpy>=1.9.1 in c:\users\varsh\anaconda3\lib\site-packages (from Theano) (1.18.1) Requirement already satisfied: scipy>=0.14 in c:\users\varsh\anaconda3\lib\site-packages (from Theano) (1.4.1) Requirement already satisfied: six>=1.9.0 in c:\users\varsh\anaconda3\lib\site-packages (from Theano) (1.14.0) Building wheels for collected packages: Theano Building wheel for Theano (setup.py) ... done Created wheel for Theano: filename=Theano-1.0.5-py3-none-any.whl size=2668124 sha256=83be88a2266c4fc3cf09784d9dd041d5e3248c24ba95472594e9536f74579df6 Stored in directory: c:\users\varsh\appdata\local\pip\cache\wheels\26\68\6f\745330367ce7822fe0cd863712858151f5723a0a5e322cc144 Successfully built Theano Installing collected packages: Theano Successfully installed Theano-1.0.5
In the above example, to install the package Theano, pip performs the following processes.
- Downloading the compressed tar file Theano-1.0.5.tar.gz
- Further building a .whl file by making a call to setup.py
- Labeling the created wheel file and then installing it successfully.
- Here, the tar.gz file retrieved by pip is a source distribution (sdist) file.
This source distribution is created when the package developer runs the following command.
python setup.py sdist
Let us now install another package, chardet. You can observe that the output varies considerably for this installation.
- Here, the pip directly downloads the .whl file from PyPI.
- Thus, you get to avoid the build stage, thereby saving time as mentioned earlier. Also, this makes wheel a ready-to-install format.
This wheel file is created when the package developer runs the following command.
python setup.py bdist_wheel
Also, to get a better understanding, do read: