pickle.PicklingError: Function to be pickled has free variables that are referenced before assignment in enclosing scope

The code I had that caused this problem looked like this:

1
2
3
4
5
6
7
if x :
y = x

def foo() :
print y

cloud.pickle(foo)

If this were a normal call to foo, the error instead would be:

1
UnboundLocalError: local variable 'y' referenced before assignment

The fix is to make sure that all variables used in foo have values at the time that it is pickled:

1
2
3
4
5
6
7
8
9
if x :
y = x
else :
y = None

def foo() :
print y

cloud.pickle(foo)

Even this would be ok:

1
2
3
4
5
6
7
8
9
def foo() :
print y

if x :
y = x
else :
y = None

cloud.pickle(foo)