Frozen modules

A frozen module is a Python module whose compiled code is built into the interpreter. Its marshalled bytecode is stored as an array of bytes in a C header file and compiled into the executable, so importing it does not need to find and read a .py or .pyc file. Python 3.11 froze the modules that are essential for startup to make interpreter startup faster (see What’s New in 3.11).

Frozen modules are imported by importlib.machinery.FrozenImporter. Their module spec has the origin "frozen", and frames from their code show up in tracebacks as <frozen os> instead of a file name. You can ask _imp whether a name is frozen with _imp.is_frozen("os").

Kinds of frozen modules

Python/frozen.c defines three groups of frozen modules.

  • Bootstrap modules are needed to set up the import system itself. They are importlib._bootstrap (frozen as _frozen_importlib), importlib._bootstrap_external (_frozen_importlib_external), zipimport and _pybuiltins (from Lib/_pybuiltins.py, frozen with the ID builtins so that tracebacks show <frozen builtins>). These are always used, even when frozen modules are turned off.

  • Stdlib modules are the standard library modules that are imported during startup. The list is grouped by why they are needed: startup without site (python -S), startup with site, the interactive interpreter, and running a module with -m. At the time of writing it contains for example os, site, io, codecs, abc, stat and runpy.

  • Test modules exist only to test the frozen module machinery. Like the stdlib modules, they are only used when frozen modules are on. They are __hello__, the package __phello__ with its submodules, a few aliases of them, and __hello_only__, which has no source module in the stdlib.

The module getpath (Modules/getpath.py) is also frozen, but it has its own rule in Makefile.pre.in and is not part of the lists above.

Finding and changing the frozen stdlib modules

The source of truth is the FROZEN list at the top of Tools/build/freeze_modules.py. Each entry has the form frozenid, frozenid : modname or frozenid : modname = pyfile, and is placed in a section. The docstring of _parse_spec() in that file describes the syntax, including how to freeze packages and their submodules. Python/frozen.c is mostly generated from this list.

To add or remove a frozen module, edit the list and run:

make regen-frozen

This runs Tools/build/freeze_modules.py. According to the notes at the top of Python/frozen.c, the script freezes the target modules, updates the includes and the module arrays in frozen.c, updates the FROZEN_FILES variable and the per-module targets in Makefile.pre.in, and updates the module lists in PCbuild/_freeze_module.vcxproj and PCbuild/_freeze_module.vcxproj.filters. Commit the changes to all of these files. The Makefile may have changed, so run make again afterwards.

The frozen data itself, in Python/frozen_modules/*.h, is always generated by the build and is not kept in the repository. Each header has a rule in Makefile.pre.in that depends on the module’s .py file, so a normal make refreshes the header after you edit the source of a frozen module. The data depends on the bytecode and marshal formats, so changes to the compiler, the bytecode or the marshal format need make regen-frozen (the comment at the top of Python/frozen.c says so).

Using frozen modules while developing

Whether frozen modules are used is a runtime setting. Set it with the -X frozen_modules option (on or off) or with the PYTHON_FROZEN_MODULES environment variable. In Python/initconfig.c the default is off for a debug build (Py_DEBUG) and on otherwise. The bootstrap modules are used either way.

When frozen modules are off, the import system uses the .py and .pyc files, which is what you want when you edit a module that would otherwise be frozen.

Frozen modules in tests

Tests that need the frozen or the non-frozen version of a module can use the helpers in Lib/test/support/import_helper.py:

  • frozen_modules(enabled=True) is a context manager that forces frozen modules on (or off with enabled=False). It only applies to modules that have not been imported yet, and the essential modules are always imported frozen.

  • import_fresh_module() and CleanImport take a usefrozen argument that defaults to False, which disables the frozen importer for the import (except for essential modules such as importlib._bootstrap).

Lib/test/test_frozen.py is a small example of a test that imports __hello__ with frozen modules enabled. The tests for the frozen importer itself are in Lib/test/test_importlib/frozen/.