Ubuntu Python: raise an exception, import 190 modules
Imagine my surprise, while writing my first PEP 302 compliant import hook this afternoon, to carefully watch sys.modules for the results of my import but see it suddenly grow by nearly two hundred modules! What on earth had I done wrong? Some quick experiments revealed that my only sin was having the temerity to raise an exception. Let's try raising a simple NameError:
>>> import sys >>> len(sys.modules) 35 >>> foo ... NameError: name 'foo' is not defined >>> len(sys.modules) 225
That's 190 extra modules — merely importing them takes around 60 ms on my laptop! Where are they all coming from? And how could an exception cause so many imports, including such illustrious modules as email, mimetools, and xml?
After reading Ubuntu's sitecustomize.py file and all of its consequences, the situation became clear. Their apport crash-reporting subsystem instruments Python with an exception hook that, when invoked, discovers that my system says enabled=0 in my /etc/default/apport file and so it undertakes no special crash logging. But, on the way to loading the routine that performs this simple check, it performs two quite flagrant and unnecessary imports, pulling in both apt (that brings with it 83 packages) and apport (an additional 107 packages).
The solution? I have removed the python-apport package, along with the ubuntuone-client suite that depends on it. After the uninstall, exceptions are — wonderfully enough — not causing a single import of a new module! Now, finally, I can continue writing my import hook in peace.