Jupyter Lab (python)

How to reload local modules in jupyter lab

Source: https://stackoverflow.com/a/54440220/10265415

A third approach is to use the `importlib.reload()` function, which allows you to reload a specific module. This is different from `%autoreload` because `importlib.reload()` also resets global variables set in the module, which might be what you want. Here is how you can use it:

    ```python
    import importlib
    importlib.reload(mymodule)
    ```
   However, this will only reload the specific module `mymodule`, and not its submodules. So, if `mymodule.data_class` has changed, you would need to reload it specifically using `importlib.reload(mymodule.data_class)`【7†source】.

Please note that there may be caveats or limitations to these methods, especially if your modules have complex dependencies or if you are using certain features of Python. For example, some objects may not be redefined even after reloading, and some changes in the module might not take effect until you restart your Python session. It is recommended to read the documentation of `%autoreload` and `importlib.reload()` to understand these caveats.

Note: This works provided that if you want to reload a submodule named mymodule.data_class, then you have to write the code- 

import importlib
importlib.reload(mymodule.data_class)

Also, if you reload before actual import, then it should give error on the first run.

Comments