Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

global imports #126

Closed
hyukim17 opened this issue Aug 20, 2015 · 3 comments
Closed

global imports #126

hyukim17 opened this issue Aug 20, 2015 · 3 comments
Milestone

Comments

@hyukim17
Copy link

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:

  1. I do not have to define a separate file for the function I am pickling
  2. I do not trigger maximum recursion depth?
@mmckerns
Copy link
Member

The answer to (1) is: use dill.settings['recurse']=True. This will likely become the default setting, however it currently is not.

I can't answer (2) as posed. Can you give an example that raises the recursion error? That would help see what needs to be done.

@mmckerns
Copy link
Member

mmckerns commented Sep 5, 2015

as far as I can tell, this is a duplicate of #123

@mmckerns mmckerns closed this as completed Sep 5, 2015
@mmckerns mmckerns modified the milestone: dill-0.2.5 Feb 6, 2016
@jessanmen1
Copy link

Just wanted to add that dill.settings['recurse']=True solved an issue like the described before while dilling scikit-learn pipelines. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants