Reproducer below:
(test) ~/repos/cloudpickle (master)❯_ ipythonPython3.9.2|packagedbyconda-forge| (default, Feb212021, 05:00:30)
Type'copyright', 'credits'or'license'formoreinformationIPython7.22.0--AnenhancedInteractivePython. Type'?'forhelp.
In [1]: fromtypesimportModuleTypeIn [2]: m=ModuleType('mod')
In [3]: exec(""" ...: def f(): ...: import math ...: return math.sqrt(4) ...: """, vars(m))
In [4]: m.f()
Out[4]: 2.0In [5]: importcloudpickleIn [6]: roundtripped_m=cloudpickle.loads(cloudpickle.dumps(m))
In [7]: roundtripped_m.f()
Out[7]: 2.0In [8]: m.f()
---------------------------------------------------------------------------ImportErrorTraceback (mostrecentcalllast)
<ipython-input-8-ada232509f74>in<module>---->1m.f()
<string>inf()
ImportError: __import__notfoundThe issue comes from the _module_reduce function, that removes the item corresponding to the __builtins__ key from the module being pickled (see #325). We should instead copy the module state before removing the item corresponding to the __builtins__ key.
Reproducer below:
The issue comes from the
_module_reducefunction, that removes the item corresponding to the__builtins__key from the module being pickled (see #325). We should instead copy the module state before removing the item corresponding to the__builtins__key.