11

What is the difference between a Python module, a built-in module, a standard module, and a frozen module?


These terms come from the official documentation itself.

Python module and built-in module are mentioned in the loader documentation:

Loaders must satisfy the following requirements:

  • If the module is a Python module (as opposed to a built-in module or a dynamically loaded extension), the loader should execute the module’s code in the module’s global name space (module.__dict__).

Standard module is referenced in the Top-Level Components documentation:

all built-in and standard modules are available

Frozen module has its own importer, which is described as

An importer for frozen modules

Despite mention in official documentation and core code, there is no definitive definition for Python module, builtin-module, standard module, or frozen module in the glossary, index (B, F, M, P, or S), or (apparently) elsewhere.


Definition: Builtin-module

Here is the best definition I could come up with for builtin-module:

A builtin-module is an extension module (i.e. written in C, Java, .NET, etc.) which is listed in the sys.builtin_module_names attribute. Note that this list may vary with the Python interpreter (i.e. a builtin-module for Cython may not be a builtin module for IronPython, etc.).

I pieced this definition together from the following sources:

The documentation for the standard library states,

The library contains built-in modules (written in C)

The importlib.machinery.BuiltinImporter documentation states,

All known built-in modules are listed in sys.builtin_module_names.

The sys.builtin_module_names attribute documentation in turn states,

the names of all modules that are compiled into this Python interpreter. (This information is not available in any other way)


Definition: frozen module*

The Python2.7 imp documentation states,

A frozen module is a module written in Python whose compiled byte-code object is incorporated into a custom-built Python interpreter by Python’s freeze utility.

It's worth noting that this definition is not present in the Python3 docs. Whether this omission is an oversight is unclear. Reference is still made in the Python3 docs to frozen modules.


I have not had any luck finding resources that help define Python module or standard module.

* Thanks to @martineau for the reference

Lorem Ipsum
  • 4,020
  • 4
  • 41
  • 67
  • 1
    This may partially help [What is a frozen Python module?](https://stackoverflow.com/questions/9916432/what-is-a-frozen-python-module) Also, a built-in module is kind of the opposite of an extension module, so saying the former _is_ the latter is self-contradictory. – martineau Jul 24 '19 at 16:50
  • Are you able to elaborate on the contradiction? The official documentation, as far as I can tell, implies that a builtin-module is an extension module, via the parenthetical remark "(written in C)". By the linked definition, an extension module must be 1) written in C and 2) use Python’s C API to interact with the core and with user code. Builtin-modules, as I've defined them, satisfy both criteria (unless they don't utilize the C API). https://docs.python.org/3.6/glossary.html#term-extension-module – Lorem Ipsum Jul 24 '19 at 17:13
  • Sure, it's simple: An extension module is one written by a third-party that extends the interpreter's capabilities, whereas as a built-in one is comes with it. – martineau Jul 24 '19 at 17:16
  • At risk of being pedantic, the following seems to imply otherwise, that a builtin-module can be an extension. It also implies that builtin-modules aren't necessarily part of the Python core (which, although not part of my definition, is a confusing way to interpret "built-in"). It states, "It is quite easy to add new built-in modules to Python, if you know how to program in C. Such extension modules..." Maybe the doc means to say "It is quite easy to add new built-in modules (via an approved pull request into the Python core)..."? https://docs.python.org/3.6/extending/extending.html – Lorem Ipsum Jul 24 '19 at 17:46

1 Answers1

-2

Modules are files containing Python definitions and statements that can be used to organize and reuse code. They help in creating a modular and organized codebase.

  • "Built-in modules" are modules that are automatically available in Python without the need for any external installation.

For example:

import os
  • "Standard modules", also known as modules from the Python Standard Library, are modules that are part of the Python distribution and cover a wide range of functionalities. They can be considered as a subset of built-in modules in Python.

For example:

import os.path

or

from datetime import datetime, timedelta
  • "Frozen modules" generally refers to modules that have been compiled into a binary format for efficient distribution or execution.

For example:

Let's say you have a python script called example_script.py

pip install pyinstaller
pyinstaller --onefile example_script.py

After running the above command, "pyinstaller" will create a "dist" directory, and inside it, you'll find the frozen executable for your script. The name of the executable will be the same as your script's name but without the .py extension.

This could be something like example_script.exe or simply example_script It is a binary file that has already been compiled.

Now, you can run the frozen executable on your machine without requiring Python or any dependencies to be installed separately. The executable will work as a standalone application.

  • "Python module" is simply a file containing Python code, saved with a .py extension.
  • 1
    "Module" is somewhat overloaded. It can be used to refer to the Python file, or the object *created by* executing the commands in a file. With that distinction in mind, build-in modules can be thought of as `module` objects defined directly in the interpreter, without going through the execute-a-series-of-Python-statements step. – chepner Jul 24 '23 at 11:39
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 27 '23 at 09:39