As part of the testing, before you import docutils, you can perform this configuration task:
import __builtin__ self.savimport = __builtin__.__import__ def myimport(name, *a): if name=='pwd': raise ImportError return self.savimport(name, *a) __builtin__.__import__ = myimport
and, of course, in the stall mode they put things back to normal:
__builtin__.__import__ = self.savimport
Explanation: all import operations go through __builtin__.__import__ , and you can reassign this name so that such operations use your own code (alternatives such as import capture are better for purposes such as importing from sources other than the file system, but for such as yours, overriding __builtin__.__import__ , as you see above, gives really simple code).
source share