Finding new and removed Python 3.11 modules in 8 lines of code

By on 9 November 2022

In our TIL Slack channel AJ shared a cool new find:

Today I learned about sys.stdlib_module_names: a frozenset of strings containing the names of standard library modules. …

AJ on the Pybites Slack – TIL (today-I-learned) channel

So I thought to give it a little test drive, specifically checking for modules that were added and removed in Python 3.11.

This is what I did:

# python3.10 REPL - pickling module names
>>> import sys
>>> with open("mods", "wb") as f:
...     pickle.dump(sys.stdlib_module_names, f)

# python3.11 REPL - compare
>>> import sys
>>> with open("mods", "rb") as f:
...     mods_310 = pickle.load(f)

# added
>>> sys.stdlib_module_names - mods_310
frozenset({'_typing', '_scproxy', '_tokenize', 'tomllib'})
# removed
>>> mods_310 - sys.stdlib_module_names
frozenset({'binhex'})

I used the pickle module to persist the frozenset that sys.stdlib_module_names returned beyond the 3.10 REPL session.

Next I went into the 3.11 REPL and loaded it back into the mods_310 variable using pickle.load().

And lastly, I used set operations to find the answer. Apart from some “internal” modules, tomllib got added and binhex got removed.

Lessons learned and thoughts:

– Look at what other Pytonistas share, it always inspires.

– Set yourself a little challenge which forces you to learn and keeps things fun (that’s how we started as Pybites!)

– Use things you previously learned (e.g. I knew you could store objects using the pickle module, and that frozensets are a type of set so they must support set operations)

– This experimenting always leads to cool new insights.

Share those insights with others so they can benefits, but apart from that, just do it for yourself to build up your reference / knowledge system.

By the way, note that pickle is not secure so consider whether you want to use it. See this article.

What Python module have you explored this week? Join our Slack and post something in our TIL channel …

Want a career as a Python Developer but not sure where to start?