The "Reload" function of importlib can force the module to be re-executed, and then the Python process of your Kernel will have the new function.
The problem is that importlib.reload
alone always works for the module (in case, your .py files) and not for the function alone -
So, if you have a file "myfile.py" and in there "def myfile(): ...",
need to do these two things - I recommend leaving a draft cell near the point where you are working with this, and re-running this cell every time you change the function:
import meuarquivo # só é necessário se "meuarquivo" não foi importado antes
from importlib import reload
reload(meuarquivo)
minhafuncao = meuarquivo.minhafuncao
Instead of the last line, you can use the import syntax as well, (but
after executing the reload
): from meuarquivo import minhafuncao
.
If you have more than one function or constants defined in the file, the assignent
in the notebook has to be made for each element you use, individually.
For example: from meuarquivo import funcao1, funcao2, df_horarios
And finally, if you don’t want to do the reassignment, you can also always use the full name (Qualified name) of anything in the file you’re updating.
So if at all points where you use the function on the notebook always use meuarquivo.minhafuncao
instead of using the from meuarquivo import funcao
, just need to make the call to Reload.
The use case informed in documentation is not the recharge of functions but rather module recharge.
– Augusto Vasques