Prevent Python from caching the imported modules

后端 未结 8 1691
误落风尘
误落风尘 2020-11-28 07:40

While developing a largeish project (split in several files and folders) in Python with IPython, I run into the trouble of cached imported modules.

The problem is th

8条回答
  •  萌比男神i
    2020-11-28 08:01

    With IPython comes the autoreload extension that automatically repeats an import before each function call. It works at least in simple cases, but don't rely too much on it: in my experience, an interpreter restart is still required from time to time, especially when code changes occur only on indirectly imported code.

    Usage example from the linked page:

    In [1]: %load_ext autoreload
    
    In [2]: %autoreload 2
    
    In [3]: from foo import some_function
    
    In [4]: some_function()
    Out[4]: 42
    
    In [5]: # open foo.py in an editor and change some_function to return 43
    
    In [6]: some_function()
    Out[6]: 43
    

提交回复
热议问题