On this page
pyclbr
Prev: py_compile | Up: stdlib_module | Next: pydoc
Rel: sys, sysconfig, builtins, warnings, dataclasses
Ref: docs.python.org — pyclbr
Tags: #public #python3 #stdlib
Import
import pyclbr
Overview
Lightweight static class and function browser (readmodule(), readmodule_ex()) that parses Python source files without importing or executing them. Use pyclbr to extract top-level class hierarchies, method names, and line numbers safely; for full syntax analysis (type annotations, decorators, expressions), prefer ast.
Quick Start
import pyclbr
# Inspect top-level classes and functions in a stdlib module without executing it
tree = pyclbr.readmodule_ex("colorsys")
assert "rgb_to_hsv" in tree
assert isinstance(tree["rgb_to_hsv"].lineno, int)
Implementation Walkthrough
1. Walking Class Definitions and Methods
Class descriptors returned by readmodule_ex() expose .name, .super, .methods (mapping method name → line number), .lineno, and .children:
import pyclbr
tree = pyclbr.readmodule_ex("cmd")
cmd_cls = tree["Cmd"]
assert isinstance(cmd_cls, pyclbr.Class)
assert "cmdloop" in cmd_cls.methods
Gotchas & Boundaries
- Minimal metadata compared to ast:
pyclbromits function signatures, docstrings, decorators, and type hints—use ast when you need more than an outline of class/function names and line numbers. - Module-name lookup:
readmodule_ex(module_name, path=...)resolves modules viasys.path(plus optionalpath=), not arbitrary file paths directly.
Common Functions
- pyclbr.readmodule() —
readmodule(module, path=None)— Return Class objects for the top-level classes in module. - pyclbr.readmodule_ex() —
readmodule_ex(module, path=None)— Return a dictionary with all functions and classes in module.
Classes & Objects
- pyclbr.Class — Information about a Python class.
- pyclbr.Function — Information about a Python function, including methods.
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/py_compile
- tuts/python3/stdlib_module/pyclbr/Class
- tuts/python3/stdlib_module/pyclbr/Function
- tuts/python3/stdlib_module/pyclbr/readmodule
- tuts/python3/stdlib_module/pyclbr/readmodule_ex
- tuts/python3/stdlib_module/pydoc
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/py_compile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pyclbr/Class Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pyclbr/Function Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pyclbr/readmodule Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pyclbr/readmodule_ex Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/pydoc Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/ast Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/builtins Linked here; shared tags: #stdlib