Claude Code for importlib.machinery: Python Import Machinery — Claude Skills 360 Blog
Blog / AI / Claude Code for importlib.machinery: Python Import Machinery
AI

Claude Code for importlib.machinery: Python Import Machinery

Published: January 26, 2029
Read time: 5 min read
By: Claude Skills 360

Python’s importlib.machinery module exposes the concrete classes that implement the standard import machinery. import importlib.machinery. Suffix lists: importlib.machinery.SOURCE_SUFFIXES[".py"], BYTECODE_SUFFIXES[".pyc"], EXTENSION_SUFFIXES[".so", ".pyd", ...]. Loaders: SourceFileLoader(name, path) — reads .py; SourcelessFileLoader(name, path) — reads .pyc; ExtensionFileLoader(name, path) — loads C extension. FileFinder: FileFinder(path, (SourceFileLoader, [".py"]), (SourcelessFileLoader, [".pyc"])) — searches one directory. PathFinder: importlib.machinery.PathFinder.find_spec(name, path) — runs the default sys.path-based search. ModuleSpec: importlib.machinery.ModuleSpec(name, loader, origin=path) — container for all metadata needed to create a module. BuiltinImporter and FrozenImporter — finders for built-in and frozen modules. Register as hook: sys.path_hooks.insert(0, lambda p: FileFinder(p, ...)) — extends .path-based import. Claude Code generates custom file finders, plugin loaders, hot-reload systems, bytecode preloaders, and import-time instrumentation.

CLAUDE.md for importlib.machinery

## importlib.machinery Stack
- Stdlib: import importlib.machinery, importlib.util, sys
- Suffixes: SOURCE_SUFFIXES / BYTECODE_SUFFIXES / EXTENSION_SUFFIXES
- Spec:   importlib.machinery.ModuleSpec(name, loader, origin=path)
- Loader: importlib.machinery.SourceFileLoader(name, path)
-         .get_source(name) / .get_code(name) / .exec_module(mod)
- Finder: importlib.machinery.FileFinder(path, (Loader, [".py"]))
- Path:   importlib.machinery.PathFinder.find_spec(name, path)
- Hook:   sys.path_hooks.insert(0, lambda p: FileFinder(p, ...))

importlib.machinery Import Pipeline

# app/importmachutil.py — spec inspect, load file, FileFinder, hot-reload, audit
from __future__ import annotations

import importlib
import importlib.machinery
import importlib.util
import sys
import os
import types
from dataclasses import dataclass, field
from pathlib import Path


# ─────────────────────────────────────────────────────────────────────────────
# 1. ModuleSpec inspection helpers
# ─────────────────────────────────────────────────────────────────────────────

@dataclass
class SpecInfo:
    name:        str
    origin:      str | None
    loader_type: str
    is_package:  bool
    submod_path: list[str]


def inspect_spec(name: str) -> SpecInfo | None:
    """
    Find and inspect the ModuleSpec for a module name without loading it.

    Example:
        info = inspect_spec("json")
        print(info.origin, info.loader_type)
        info = inspect_spec("os.path")
    """
    try:
        spec = importlib.util.find_spec(name)
    except (ModuleNotFoundError, ValueError):
        return None
    if spec is None:
        return None
    loader_type = type(spec.loader).__name__ if spec.loader else "None"
    sub = list(spec.submodule_search_locations) \
        if spec.submodule_search_locations else []
    return SpecInfo(
        name=spec.name,
        origin=spec.origin,
        loader_type=loader_type,
        is_package=bool(spec.submodule_search_locations),
        submod_path=sub,
    )


def list_installed_suffixes() -> dict[str, list[str]]:
    """
    Return the file suffixes recognised by the import machinery.

    Example:
        suf = list_installed_suffixes()
        print(suf["source"])   # [".py"]
    """
    return {
        "source":    importlib.machinery.SOURCE_SUFFIXES,
        "bytecode":  importlib.machinery.BYTECODE_SUFFIXES,
        "extension": importlib.machinery.EXTENSION_SUFFIXES,
    }


# ─────────────────────────────────────────────────────────────────────────────
# 2. Load a module from an arbitrary file path
# ─────────────────────────────────────────────────────────────────────────────

def load_source_file(path: "str | Path",
                     name: str | None = None) -> types.ModuleType:
    """
    Load a Python source file as a module using SourceFileLoader.
    If name is None, derives the module name from the filename.

    Example:
        mod = load_source_file("/path/to/plugin.py")
        print(mod.__name__, dir(mod))
    """
    path = Path(path)
    if name is None:
        name = path.stem
    loader = importlib.machinery.SourceFileLoader(name, str(path))
    spec = importlib.machinery.ModuleSpec(name, loader, origin=str(path))
    mod = importlib.util.module_from_spec(spec)
    sys.modules[name] = mod   # register first to support circular imports
    try:
        loader.exec_module(mod)
    except Exception:
        sys.modules.pop(name, None)
        raise
    return mod


def get_module_source(name: str) -> str | None:
    """
    Return the source code for an already-importable module, if available.

    Example:
        src = get_module_source("json")
        print(src[:200])
    """
    try:
        spec = importlib.util.find_spec(name)
    except (ModuleNotFoundError, ValueError):
        return None
    if spec is None or spec.origin is None:
        return None
    loader = spec.loader
    if not isinstance(loader, importlib.machinery.SourceFileLoader):
        return None
    try:
        return loader.get_source(name)
    except Exception:
        return None


# ─────────────────────────────────────────────────────────────────────────────
# 3. FileFinder — search a directory for Python files
# ─────────────────────────────────────────────────────────────────────────────

def find_module_in_dir(directory: "str | Path",
                       module_name: str) -> str | None:
    """
    Look for module_name in a single directory using FileFinder.
    Returns the file path if found, or None.

    Example:
        path = find_module_in_dir("/usr/lib/python3/dist-packages", "email")
    """
    directory = str(directory)
    finder = importlib.machinery.FileFinder(
        directory,
        (importlib.machinery.SourceFileLoader,
         importlib.machinery.SOURCE_SUFFIXES),
        (importlib.machinery.SourcelessFileLoader,
         importlib.machinery.BYTECODE_SUFFIXES),
    )
    try:
        spec = finder.find_spec(module_name)
        if spec is not None:
            return spec.origin
    except Exception:
        pass
    return None


# ─────────────────────────────────────────────────────────────────────────────
# 4. Hot-reload a module from disk
# ─────────────────────────────────────────────────────────────────────────────

def hot_reload(name: str) -> types.ModuleType | None:
    """
    Force a reload of a module from its source file.
    Returns the reloaded module, or None if not found.

    Example:
        import mymodule
        # edit mymodule.py on disk …
        mymodule = hot_reload("mymodule")
    """
    mod = sys.modules.get(name)
    if mod is None:
        return None
    spec = getattr(mod, "__spec__", None)
    if spec is None or spec.loader is None:
        return None
    try:
        spec.loader.exec_module(mod)
        return mod
    except Exception:
        return None


# ─────────────────────────────────────────────────────────────────────────────
# 5. Full meta_path loader audit
# ─────────────────────────────────────────────────────────────────────────────

@dataclass
class MetaPathEntry:
    index:      int
    type_name:  str
    module:     str


def audit_meta_path() -> list[MetaPathEntry]:
    """
    Return information about every finder on sys.meta_path.

    Example:
        for entry in audit_meta_path():
            print(entry.index, entry.type_name)
    """
    entries: list[MetaPathEntry] = []
    for i, finder in enumerate(sys.meta_path):
        entries.append(MetaPathEntry(
            index=i,
            type_name=type(finder).__name__,
            module=type(finder).__module__,
        ))
    return entries


@dataclass
class PathHookEntry:
    index:     int
    type_name: str


def audit_path_hooks() -> list[PathHookEntry]:
    """Return information about every hook on sys.path_hooks."""
    entries: list[PathHookEntry] = []
    for i, hook in enumerate(sys.path_hooks):
        name = getattr(hook, "__name__",
                       getattr(hook, "__class__").__name__)
        entries.append(PathHookEntry(index=i, type_name=name))
    return entries


# ─────────────────────────────────────────────────────────────────────────────
# Demo
# ─────────────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    import tempfile

    print("=== importlib.machinery demo ===")

    # ── suffixes ──────────────────────────────────────────────────────────────
    print("\n--- file suffixes ---")
    for k, v in list_installed_suffixes().items():
        print(f"  {k:10s}: {v}")

    # ── inspect_spec ──────────────────────────────────────────────────────────
    print("\n--- inspect_spec ---")
    for mod_name in ["json", "os.path", "sys", "_json", "email"]:
        info = inspect_spec(mod_name)
        if info:
            print(f"  {mod_name:15s}  loader={info.loader_type:30s}  "
                  f"pkg={info.is_package}")

    # ── load_source_file ──────────────────────────────────────────────────────
    print("\n--- load_source_file ---")
    with tempfile.NamedTemporaryFile(suffix=".py", mode="w",
                                     delete=False) as tf:
        tf.write("ANSWER = 42\ndef greet(name): return f'Hi, {name}!'\n")
        tmp_path = tf.name

    try:
        mod = load_source_file(tmp_path, "demo_mod")
        print(f"  ANSWER  = {mod.ANSWER}")
        print(f"  greet   = {mod.greet('world')!r}")
    finally:
        os.unlink(tmp_path)
        sys.modules.pop("demo_mod", None)

    # ── get_module_source ─────────────────────────────────────────────────────
    print("\n--- get_module_source (json) ---")
    src = get_module_source("json")
    if src:
        lines = src.splitlines()
        print(f"  {len(lines)} lines; first non-blank: {next(l for l in lines if l.strip())!r}")

    # ── audit meta_path ───────────────────────────────────────────────────────
    print("\n--- audit_meta_path ---")
    for entry in audit_meta_path():
        print(f"  [{entry.index}]  {entry.type_name}")

    # ── audit path_hooks ──────────────────────────────────────────────────────
    print("\n--- audit_path_hooks ---")
    for entry in audit_path_hooks():
        print(f"  [{entry.index}]  {entry.type_name}")

    print("\n=== done ===")

For the importlib.abc stdlib companion — importlib.abc.MetaPathFinder + Loader define the abstract interfaces that importlib.machinery implements concretely — use importlib.abc when writing custom finders and loaders; use importlib.machinery when you need to instantiate, inspect, or extend the standard file-system loaders (SourceFileLoader, FileFinder, ExtensionFileLoader). For the importlib.util stdlib companion — importlib.util.find_spec(name) calls PathFinder and returns the ModuleSpec without loading the module; importlib.util.module_from_spec(spec) creates an empty module object from a spec — use importlib.util for safe high-level find-without-load and module creation; use importlib.machinery when you need direct access to the loader classes themselves to set up FileFinder hooks or read source from a loader. The Claude Skills 360 bundle includes importlib.machinery skill sets covering SpecInfo/inspect_spec() metadata reader, list_installed_suffixes() suffix table, load_source_file() path-based loader, get_module_source() source reader, find_module_in_dir() FileFinder wrapper, hot_reload() exec_module reloader, and MetaPathEntry/PathHookEntry/audit_meta_path()/audit_path_hooks() auditors. Start with the free tier to try import machinery patterns and importlib.machinery pipeline code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free