1

I have a script I'm trying to run from the command line:

import ipdb
def f(): pass
import pickle
pickle.dumps(f)

This is of course simplified. In reality I'm trying to use f in a multiprocessing.Pool, hence the need to pickle it. With ipython 5.3.0 and ipdb 0.10.2 this yields:

PicklingError: Can't pickle <function f at 0x1071eaaa0>: it's not found as __main__.f

I believe this is because IPython is overwriting sys.modules['__main__'] as described here.

I'd like to work around this problem. One idea I had was to replace sys.modules['__main__'] with the current module. I could obtain this reference before importing ipdb, but I'd like to avoid that (for rather involved reasons beyond the scope of this question).

Is it possible to get a reference to the current module after it's been overwritten in sys.modules? I see that I can get its name (__main__ in my case) but I don't see how to get its value (other than by looking in sys.modules).

martineau
  • 119,623
  • 25
  • 170
  • 301
nonagon
  • 3,271
  • 1
  • 29
  • 42

0 Answers0