pythonjupyter-notebook

Jupyter (VSCode) doesn't recognize changes in self-made modules/packages


Whenever I change something in my own packages and reload the import in JupyterNotebook the change is not taken over by Jupyter.

Currently I copy the function from the package into a cell and rerun it there. But this is messy and obviously not a nice solution. And don't want to restart my whole kernel as I work with data sets and other packages that need even some time to load.

I understand that

%reload_ext autoreload
%autoreload 2

solves my problem, but only if I load my modules directly. But I prefer to load function from my modules instead of the whole module.


Solution

  • I use importlib to achieve the same. I find if I import the parent module, reloading will also update the functions I specifically imported from that module, so I can use the more conveniently named functions.

    import importlib
    import mymodule
    import my_utils
    from my_utils import somefunction, anotherfunction
    
    def module_reload():
        importlib.reload(mymodule)
        importlib.reload(my_utils)   
    

    And then I can use in later cells as appropriate:

    module_reload()
    
    somefunction()
    anotherfunction()