Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of Python Wheels?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "what is the role of Python Wheels", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "what is the role of Python Wheels" bar!

Python .whl files (or wheels) are part of python that is rarely mentioned, but they are important to the installation process of the python package. If you have installed the python package using pip, there is a good chance that wheels makes your installation faster and more efficient.

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

Brief introduction of Wheel

Before learning how to package a project into a wheel, it is helpful to understand what it is like to use a wheel from the user's point of view.

You can start this experiment by installing a Python package in your environment as usual. In this case, install uWSGI version 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 uwsgi10 Installing collected packages: uwsgi11 Successfully installed uwsgi-2.0.18

To fully install uWSGI, pip takes several different steps:

On line 3, it downloads a TAR file (tarball) called uwsgi-2.0.18.tar.gz, which is compressed with gzip.

On line 6, it accepts tarball and builds a .whl file by calling setup.py.

In line 7, it marks the wheel as uWSGI-2.0.18-cp38-cp38-macosx_10_15_x86_64.whl.

In line 10, it installs the actual package after building the wheel.

The tar.gz tarball retrieved by pip is a source distribution, or sdist, rather than a wheel. In some ways, sdist is the opposite of wheel.

The source code distribution contains the source code. This includes not only the Python code, but also the source code (usually written in C or C++) of any extension modules that are bound to the package. For the source distribution, the extension module is compiled on the client side, not on the developer side.

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

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

$python setup.py sdist

Now try to install 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 the uWSGI installation.

Download a .whl file directly from PyPI when you install chardet. The wheel name is chardet-3.0.4-py2.py3-none-any. Whl follows a specific naming convention, as 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 the 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? You can find out why by looking at the page of each project on PyPI and navigating to the download area. This section will show you what pip actually sees on the PyPI index server:

Due to the complexity of the project, uWSGI provides only one source distribution (uWSGI-2.0.18.tar.gz).

Chardet provides both a roulette and a source code distribution, but pip will prefer roulette if it is compatible with your system. You'll see how to determine compatibility later.

Another example of a compatibility check for wheel installation is psycopg2, which provides a large number of wheels for Windows but no wheels for Linux or macOS clients. This means that depending on your settings, pip installation psycopg2 can get a scroll wheel or source distribution.

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

So far, you have seen some obvious differences between wheels and sdist, but more importantly, the impact of these differences on the installation process.

Wheel acceleration installation

Above, you saw a comparison between getting the installation of prefabricated wheels and downloading the installation of sdist. Wheels make the end-to-end installation of Python packages faster for two reasons:

Other things being equal, wheels are usually smaller than source distributions, which means they can move faster across the network.

Installing directly from wheels avoids the intermediate step of building the package from the source distribution.

It is almost guaranteed that installing chardet takes only a fraction of the time it takes to install uWSGI. However, 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 demonstrates how much difference the wheel makes.

You can use the-no-binary option to make pip ignore its tilt to the wheel:

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

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

On my machine, it takes about 32 seconds from start to finish. Not only does installation take a long time, but building encryption also requires the provision of OpenSSL development headers, which can be used in Python.

Cryptography can now be reinstalled, but this time make sure that pip uses wheels from PyPI. Because pip prefers wheels, this is similar to calling pip install with no parameters at all. But in this case, you can let the intention explicitly pass the required wheel-pure binary:

This option takes a little more than 4 seconds, which is only 1/8 of the time it takes for cryptography and its dependencies to use the source distribution.

Thank you for your reading, the above is the content of "what is the role of Python Wheels", after the study of this article, I believe you have a deeper understanding of the role of Python Wheels, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report