x = 1

def local_func():
    return x
local_func.__globals__.update(locals())
local_func()
x = 42
local_func()
local_func.__globals__.update(locals())
local_func()
How to make imports / closures work from IPython's embed?