开发者

Python shell and execfile scope

开发者 https://www.devze.com 2023-01-12 19:27 出处:网络
I\'m working from inside an ipython shell and often need to reload the script files that contain my functions-under-construction.

I'm working from inside an ipython shell and often need to reload the script files that contain my functions-under-construction.

Inside my main.py I have:

def myreload(): execfile("main.py") execfile("oth开发者_运维百科erfile.py")

Calling myreload() works fine if I have already ran in the same ipython session the execfile commands directly.

However, for some reason, if the session is fresh and I just called execfile("main.py"), then myreload() doesn't actually make the functions from inside otherfile.py available. It doesn't throw any error though.

Any ideas?


Functions create a new scope. execfile() runs the script in the current scope. What you are doing will not work.


If you seek to learn the actual correct usage of ipython (i.e. in interactive mode), then you should use magic commands like:

%run

and

%edit

Check help for those functions with %run?. This provides explicit examples.

See also http://ipython.org/ipython-doc/stable/interactive/tutorial.html

0

精彩评论

暂无评论...
验证码 换一张
取 消