Springe zum Hauptinhalt Profilbild Hartmut Goebel

Hartmut Goebel

Diplom-Informatiker, CISSP, CSSLP, ISO 27001 Lead Implementer



Anfrage
Logo Goebel Consult

Debugging Python _frozen_importlib

``_frozen_importlib`` has no source available, anyhow you can comfortably step through by using a trick.

Today I fund myself debugging some strange import issues and thus needing to step through _frozen_importlib also known as importlib._bootstrap.

Well. as the name says, this module is frozen, which – amongst others – means: the bytecode is not connected the some source, effectively inhibiting debugging. One can still step through the code of the frozen module. Anyhow, since there the debugger is not able to show the source line, this become really hard.

So my idea was to replace the _frozen_importlib module by the variant where the source is available, which it importlib._bootstrap. Here is how I did it near the top of my Python script:

import sys, _imp
# remove the frozen module
del sys.modules['importlib']._bootstrap
del sys.modules['_frozen_importlib']
del sys.modules['importlib._bootstrap']
# import the non-frozen module
import importlib._bootstrap
# … and set it up as importlib/__init__.py does
importlib._bootstrap._setup(sys, _imp)