Quick tracing

28 April 2014 (updated 20 September 2018)

Here's a quick way to see what code is actually run when calling some library.

Just create a file shell.py with this in it:

import code
con = code.InteractiveConsole()


code, codeop, __future__ modules are ignored so you don't see the trace for the code handling the REPL.

Then run:

python -mtrace --ignore-module=code,codeop,__future__ --trace shell.py


>>> os.path.isfile('/')
 --- modulename: utf_8, funcname: decode
utf_8.py(16):     return codecs.utf_8_decode(input, errors, True)
 --- modulename: genericpath, funcname: isfile
genericpath.py(28):     try:
genericpath.py(29):         st = os.stat(path)
genericpath.py(32):     return stat.S_ISREG(st.st_mode)
 --- modulename: stat, funcname: S_ISREG
stat.py(50):     return S_IFMT(mode) == S_IFREG
 --- modulename: stat, funcname: S_IFMT
stat.py(25):     return mode & 0170000

This entry was tagged as debugging python