how to “reimport” module to python then code be changed after import

前端 未结 4 713
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 12:37

I have a foo.py

def foo():
    print \"test\"

In IPython I use:

In [6]:  import foo
In [7]:  foo.foo()
test
         


        
4条回答
  •  抹茶落季
    2020-12-02 13:22

    IPython3's autoreload feature works just right.

    I am using the actual example from the webpage. First load the 'autoreload' feature.

    In []: %load_ext autoreload
    In []: %autoreload 2
    

    Then import the module you want to test:

    In []: import foo
    In []: foo.some_function()
    Out[]: 42
    

    Open foo.py in an editor and change some_function to return 43

    In []: foo.some_function()
    Out[]: 43
    

    It also works if you import the function directly.

    In []: from foo import some_function
    In []: some_function()
    Out[]: 42
    

    Make change in some_function to return 43.

    In []: some_function()
    Out[]: 43
    

提交回复
热议问题