Debugging Python _frozen_importlib
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)