Springe zum Hauptinhalt

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 na­me says, this mo­du­le is fro­zen, which – amongst others – means: the by­te­co­de is not connec­ted the some sour­ce, ef­fec­tive­ly in­hi­bi­ting de­bug­ging. One can still step through the code of the fro­zen mo­du­le. Any­how, sin­ce the­re the de­bug­ger is not ab­le to show the sour­ce li­ne, this be­co­me re­al­ly 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)
Portrait von Hartmut Goebel

Hartmut Goebel

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

Haben Sie noch Fragen?
Anruf oder Mail genügt:
  +49 871 6606-318
  +49 175 29 78 072