I'm trying to load functions from a script dynamically when I'm inside an ipython interactive shell. For example, suppose I have a python script like this:
# script.py
import IPython as ip
def Reload():
execfile('routines.py', {}, globals())
if __name__ == "__main__":
ip.embed()
Suppose the file routines.py is like this:
# routines.py
def f():
print 'help me please.'
def g():
f()
Now if I run the script script.py, I'll be entering the interactive shell. If I type the following, my call to g() works:
execfile('routines.py')
g()
However, if I type the following, the call to g() fails:
Reload()
g()
I will get an error message saying that "global name f is not defined.", although I can still see that f and g are in the output when I type globals() in the interactive shell.
What's the difference of these two?
UPDATE:
The following works, however it's not a preferred solution so I would like to have a better solution for the problem above.
If I change script.py to:
# script.py
import IPython as ip
def Reload():
execfile('routines.py')
if __name__ == "__main__":
ip.embed()
And change routines.py to:
# routines.py
global f
global g
def f():
print 'help me please.'
def g():
f()
Then if I call Reload() in the interactive shell and then call g(), it works. However this is not a preferred approach because I have to declare global names.
UPDATE 2:
It seems that the problem is independent of ipython. With the first version of routines.py if I start the python shell, and type the following by hand:
def Reload():
execfile('routines.py', {}, globals())
g()
The call to g() also fails. But the following works:
execfile('routines.py')
g()