6

Imagine you have created a py file named funcs with a function called my_func().

While using Jupyter Notebook, you want to import said function with the following:

from funcs import my_func

Now you do something with it, i.e.

x = my_func(bla)

After that, there is something that you want to modify from my_func. So I change it and save it. If I re-run the code posted above, the change will not be seen, and therefore I am obliged to restart the kernel to run the function with said changes. Is there an efficient way to do it?

Note: I know the reload module allows you to reload the entire funcs py file, but I want to do this in a more specific function level. Not the entire script. With reload I would have to import funcs and not just my_func and therefore would have to write

x = funcs.my_func(bla)

Which isn't my objective.

Answer:

import sys, importlib
importlib.reload(sys.modules['funcs])
from funcs import my_func
4
  • I actually saw that question, the problem there is that you are reloading all the module. I would have to do x = funcs.my_func() and also import funcs and not just the specific python function within the file, which makes it less efficient for my particular type of issues. Commented Mar 1, 2019 at 18:34
  • I have edited in order to show this difference. Thank you Commented Mar 1, 2019 at 18:36
  • Ok then refer to this post. Commented Mar 1, 2019 at 18:41
  • You are correct, it is a duplicate. I mark it as duplicate. Commented Mar 1, 2019 at 18:42

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.