Python cold knowledge: how to find out which standard libraries have been added or deleted in the new version?

"Battery built-in" is one of the most notable features of Python, which provides more than 200 standard libraries out of the box. However, after more than 30 years of development, many standard libraries have become historical burdens that have to be discarded, because they are "leakage"!

The good news is that Python is undergoing a "slimming operation", details can be found at:

So, we will have such a topic: When Python releases a new version, how to find out which standard libraries it has added or deleted compared to the previous version (or earlier version)?

For example, when Python releases version 3.11.1, how do you find out which standard libraries have been added or deleted compared to the previous version (ie 3.11.0)?

Maybe you immediately thought of a way: check the official version change document~

That's right, the official documents definitely contain the change information we need, but there is too much information in each version of " What's New ", and this kind of search without a specific goal will only waste time and effort.

If you want to compare across multiple versions, such as the difference between 3.12 and 3.10, or compare the future 3.x with the current 3.11, this method is even more difficult to use!

Before version 3.10, it was really inconvenient to know the changes in the standard library. However, since 3.10, Python provides a very convenient method: sys.stdlib_module_names!

Description from the official documentation:

Source: https://docs.python.org/zh-cn/3/library/sys.html?#sys.stdlib_module_names

Simply look at its contents:

As can be seen above, sys.stdlib_module_nameswhat is returned is an object of type frozenset whose elements are the names of all standard libraries.

With a detailed standard library list, we can compare the differences between different Python versions through the following steps:

(1) Obtain the old version of the standard library (such as 3.10.0), and store it in a file/database after serialization

>>> import sys
>>> import pickle
>>> with open("libs", "wb") as f:
...     pickle.dump(sys.stdlib_module_names, f)
...

(2) Get a new version of the standard library (such as 3.11.0) and compare it with the old version of the standard library

>>> import sys
>>> import pickle
>>> with open("libs", "rb") as f:
...     old_libs = pickle.load(f)
...
>>> sys.stdlib_module_names - old_libs
frozenset({'_typing', '_scproxy', '_tokenize', 'tomllib'})
>>> old_libs - sys.stdlib_module_names
frozenset({'binhex'})

From the above example, we can know that compared with 3.10, 3.11 has increased _typing, _scproxy, _tokenizeand tomllib, and it has also decreased one binhex.

With just a few lines of code, this method is much more convenient and accurate than flipping through complicated documents.

It is worth noting that this sys.stdlib_module_namesis a new feature of version 3.10. Before it, there was a similar one sys.builtin_module_names, but it returned only the built-in modules used by the interpreter:

So, what is the use of this new feature other than the use of knowing the deletion of the Python standard library mentioned above? In other words, why did the Python official suddenly add sys.stdlib_module_namesthis feature?

Original link: https://mp.weixin.qq.com/s/NoZniWQU3dUA_0TmZ2kHzw

In fact, there is a tripartite library in the community stdlib-list, which can be used to obtain the standard library list of some Python versions (2.6-2.7; 3.2-3.9). The author of this library mentioned his appeal in the documentation , and also mentioned that other developers have the same appeal :

Victor Stinner, the core developer who developed sys.stdlib_module_namesthis feature, also summarized several usage scenarios:

Judging from these usage scenarios, sys.stdlib_module_namesthe role of the game is really not small. Also, at the time of writing this article, I discovered from CPython's Issue #87121 that the well-known machine learning library pytorchalso needs this feature.

pytorchThe standard library list of each Python version was hard-coded, and the code was lengthy. Now it has been adapted to use a new method , which greatly facilitates subsequent maintenance:

On November 15th, Python 3.12 alpha 2 was released, and this version began to remove a lot of obsolete and deprecated content (annotation library, submodules of the standard library, classes and functions, etc.). Interested students can use the "cold knowledge" introduced in this article to see what changes have occurred~

First published in Python cat , if you need to reprint, please contact the author

Zhihu: Python cat

Blog Garden: Cats Under Pea Flowers

Nuggets: The Cat Under the Pea Blossom

CSDN: Python cat

Guess you like

Origin blog.csdn.net/chinesehuazhou2/article/details/127934854