3

I am trying to create a GCP cloud function which loads a pickled python object for use.

The pickle is stored as a byte string.

The function fails when loading the pickle with the error: "Can't get attribute 'MyClass' on <module 'google.cloud.functions.worker' from '/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py'>"

From that I am assuming the issue is due to namespace, as when the object is created it is stored in <module '__main__'>

I found Unable to load files using pickle and multiple modules which is useful but I don't think either of the solutions would be possible to implement in cloud functions.

How would I go about loading a pickled object which was pickled outside of the function?

N.B I do have the MyClass class in the function.

Charlie Morton
  • 737
  • 1
  • 7
  • 17

1 Answers1

1

The problem is google cloud functions is actually importing and calling your code from its own __main__. so while you have Myclass in your main.py it will show up as main.Myclass when you pickled file is expecting __main__.Myclass. I was able to hack it by simply adding the class directly to the __main__ module by doing setattr(sys.modules["__main__"],'MyClass',MyClass) before you load your pickled file.

Austen Novis
  • 444
  • 1
  • 12
  • 30