How could we un-import my user-defined package?


I wrote a user-defined package, which contains some functions

def function1(x, y, ....):

I import the package and use it,

import my_own_package

and from time to time I need to revise the functions in the package. I work on Jupiter notebook, and whenever I revise the package, I need to close the Jupiter notebook in order to run the new version of the package. If I do not close it, I’d be able to run it, and some times I even see codes of the new version of the package in the error message. I thought I was running the right version, but I do not know why I got the unexpected error message again and again. The only way I know to tackle this issue is to close the Jupiter notebook and reopen it.
Is there any easier way to unimport and reimport the revised version of the package? I tried this, but it did not work

del my_own_package
import my_own_package

even though I can successfully import the package after the “del” command.
Any suggestion would be highly appreciated!


You don’t have to close the notebook, but you should re-start the notebook kernel (it should be an option from the “Kernel” menu).

It’s not impossible to “reload” a live module using importlib.reload, but it is generally a bad idea, and I would say even more so in a Jupyter notebook where program state is smeared across multiple cells. It is a recipe for inviting confusion (just restart the kernel).

1 Like

What you’re really asking for, here, is rerunning a package. I would recommend rethinking the way you do this; there are a few ways to conceptualize what you’re doing:

  1. Rerun everything from scratch (restart the notebook kernel, reinitialize). This wipes out what you have, starts fresh, and lets you work from a clean slate. Consistent, dependable, but possibly overkill if you have a lot of initialization to redo.
  2. Re-execute a specific script, which provides certain functions. Conceptually equivalent to copying and pasting a big block of code from straight into the notebook, but could be done with a function that basically exec’s the source code in the globals.
  3. (Not recommended) Keep the import, but use importlib.reload to force it to be reloaded.

Choose whichever one works best for you. I find that restarting from scratch is usually the safest option, but if the slowness of restarting is a pain, then exec’ing a bunch of code any time you edit it will be a bit easier. It takes some care, though.

1 Like

I’m going to suggest that Jupyter notebooks are not the right tool for when you are developing a package / module – if you have to update and reload a utility module once in a while, just restart the kernel as suggested.

If you are actively updating a module, don’t try to run it by importing it into a Jupyter notebook. Run it with unit tests, or a test script from a command line, or the iPython “run” command (you may be able to use “run” from a notebook too – I haven’t tried that

Jupyter is a great tool for interactive data analysis – but not a great one for developing Python modules.


Shameless plug (I’m one of the volunteer maintainers), but Spyder has a built-in User Module Reloader to automatically reload your modules when you change them and re-run your script. It’s gotten to the point where it’s generally very reliable in regular use, unlike manual hacks.

Spyder might be a better choice overall if you’ve gotten to the point where you’re developing your own modules, as it supports most of the same interactive features as Jupyter (and a number more), has a built-in IPython console that can connect to Jupyter kernels, can import notebooks or interact with them directly via the spyder-notebooks plugin, and has cell-based execution support with standard Python modules and scripts rather than requiring bespoke binary notebooks.

1 Like

I recommend autoreload (autoreload — IPython 3.2.1 documentation) which takes care of reimporting the function for you.

1 Like