Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to reload a module in python to capture changes?

I am running python 3.6.4 (anaconda, spyder).

Do I need to reload a user-defined module in order to capture the changes?

For example, suppose I wrote the simple function and save it in test.py file:

def plus5(x):
    return x + 5

Then in the IPython console I type

import test as t

and then I change the user-defined function to:

def plus5(x):
    return x + 500

Then when I type in IPython console

t.plus5(0)

it returns 500 without re-importing or reloading the module first.

If I change the function name from plus5 to something else then I have to re-import the module to see the change. But when I change the function statements then it automatically captures the changes without re-importing the module

From the Python documentation:

Note: For efficiency reasons, each module is only imported once per interpreter session. Therefore, if you change your modules, you must restart the interpreter – or, if it’s just one module you want to test interactively, use importlib.reload()

e.g. import importlib; importlib.reload(modulename).

like image 699
gnikol Avatar asked Apr 09 '26 11:04

gnikol


1 Answers

This is a feature in the IPython interpreter name autoreload. It has the magic command %autoreload which allows for activating or deactivating this feature. It seems to be on by default, but I was not able to find something proving that.

like image 88
MegaIng Avatar answered Apr 11 '26 00:04

MegaIng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!