You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I know that dill can have issues pickling global variables, but I was wondering if the following case is intended. My use case for this is setting up a job on a given machine, pickling the instructions, sending it over to remote machines, unpickling the objects, and running the jobs there.
Run the following in one file.
import dill
import numpy as np
def test():
return np
if __name__ == "__main__":
with open("out", "w") as f:
f.write(dill.dumps(test))
Run the next in a different file. Running in a different file is necessary to emulate the environment I am using, where the numpy import is not included at the target machine.
import dill
with open("out") as f:
contents = f.read()
res = dill.loads(contents)
print(res())
This yields:
NameError: global name 'np' is not defined
However, the code works if, instead of the first file, I define the function test in a separate file (so that it is in a formal namespace, not "main", load and pickle it from a separate runner, the numpy import also gets packaged and unpickled successfully.
In other words, if we have a file
import numpy as np
def test():
return np
and we have a separate file
from source import test
with open("out", "w") as f:
f.write(dill.dumps(test))
then the unpickling is fine.
As it turns out, setting dill.settings["recurse"] = True fixes this particular example, but in my actual script, which is more complicated, doing so raises:
RuntimeError: maximum recursion depth exceeded in cmp
Can I get global imports such that:
I do not have to define a separate file for the function I am pickling
I do not trigger maximum recursion depth?
The text was updated successfully, but these errors were encountered:
I know that dill can have issues pickling global variables, but I was wondering if the following case is intended. My use case for this is setting up a job on a given machine, pickling the instructions, sending it over to remote machines, unpickling the objects, and running the jobs there.
Run the following in one file.
Run the next in a different file. Running in a different file is necessary to emulate the environment I am using, where the numpy import is not included at the target machine.
This yields:
However, the code works if, instead of the first file, I define the function
test
in a separate file (so that it is in a formal namespace, not "main", load and pickle it from a separate runner, the numpy import also gets packaged and unpickled successfully.In other words, if we have a file
and we have a separate file
then the unpickling is fine.
As it turns out, setting
dill.settings["recurse"] = True
fixes this particular example, but in my actual script, which is more complicated, doing so raises:Can I get global imports such that:
The text was updated successfully, but these errors were encountered: