561

How do I reimport a module? I want to reimport a module after making changes to its .py file.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
  • 4
    Possible duplicate of [How do I unload (reload) a Python module?](http://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module) – Walter May 18 '17 at 09:33
  • [reimport python module](https://stackoverflow.com/a/437591/6521116) – LF00 Jun 10 '17 at 01:19

7 Answers7

562

For Python 3.4+:

import importlib
importlib.reload(nameOfModule)

For Python < 3.4:

reload(my.module)

From the Python docs

Reload a previously imported module. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter.

Don't forget the caveats of using this method:

  • When a module is reloaded, its dictionary (containing the module’s global variables) is retained. Redefinitions of names will override the old definitions, so this is generally not a problem, but if the new version of a module does not define a name that was defined by the old version, the old definition is not removed.

  • If a module imports objects from another module using from ... import ..., calling reload() for the other module does not redefine the objects imported from it — one way around this is to re-execute the from statement, another is to use import and qualified names (module.*name*) instead.

  • If a module instantiates instances of a class, reloading the module that defines the class does not affect the method definitions of the instances — they continue to use the old class definition. The same is true for derived classes.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Benjamin Wohlwend
  • 30,958
  • 11
  • 90
  • 100
  • 63
    if I load my modules using `from filename import *`. How to reload? – Peter Zhu Jul 23 '15 at 05:22
  • 11
    @PeterZhu for the "from mod import fcn" case, the module object is not added to the name space. So you need to explicitly import the module, so you can later reload it. `import foo; from foo import bar; reload(foo)` – Ted Jun 05 '18 at 23:59
  • 1
    I tried the reload, as well as the autoreload magic, and I see my code change reflected in the error message's stack, but the error itself still appears to be off the stale code (e.g., the line of the error is on the exact same line as before, which I have changed to a comment, and a comment clearly cannot be causing errors). My Module is loaded in as a Jupyter Extension, might anyone know if this requires a different work around? Thanks! – yifanwu Jul 30 '19 at 20:21
  • 4
    If you imported your function as following "import mod as name", then you need to do as follows: 'import importlib importlib.reload(name)' – Noe Jan 06 '20 at 23:34
  • This method might not override other modules' references to the reloaded module. See https://stackoverflow.com/a/61617169/2642356 for a solution to that. – EZLearner May 05 '20 at 15:51
  • What if something like `from pylibdmtx.pylibdmtx import decode` ? Is `import pylibdmtx; from pylibdmtx.pylibdmtx import decode; importlib.reload(pylibdmtx.pylibdmtx)` right? – Junfeng Oct 26 '21 at 07:26
  • I think `importlib.reload(nameOfModule)` is confusing. `reload()` takes a module object, not a string, e.g., `importlib.reload(foo)` instead of `importlib.reload('foo')`. Maybe `importlib.reload(myModule)` would be less ambiguous. – Christoph Thiede Jan 27 '23 at 18:27
  • and what if you import from your own module, eg. from folder.sub_folder.functions import * – tararuj4 May 31 '23 at 09:41
354

In python 3, reload is no longer a built in function.

If you are using python 3.4+ you should use reload from the importlib library instead:

import importlib
importlib.reload(some_module)

If you are using python 3.2 or 3.3 you should:

import imp  
imp.reload(module)  

instead. See http://docs.python.org/3.0/library/imp.html#imp.reload

If you are using ipython, definitely consider using the autoreload extension:

%load_ext autoreload
%autoreload 2
Andrew
  • 4,289
  • 2
  • 28
  • 33
  • 1
    @Andrew thanks! I used the %autoreload, it's wonderful, my already created objects got automatically the corrected implementation of the class methods without having to recreate them – jeanmi Apr 03 '19 at 15:17
  • 5
    I'm a bit late, but I think this does not work if what you need to reload is a function or class from within the module: if my import statment was `from mymodule import myfunc`, then `importlib.reload(myfunc)`, `importlib.reload(mymodule.myfunc)`, `importlib.reload(mymodule)` all give a NameError. – Puff Jul 11 '19 at 22:40
  • 1
    @Puff see [my answer below](https://stackoverflow.com/a/60936171/2514130) for how to re-import a function – jss367 Mar 30 '20 at 17:34
46

Actually, in Python 3 the module imp is marked as DEPRECATED. Well, at least that's true for 3.4.

Instead the reload function from the importlib module should be used:

https://docs.python.org/3/library/importlib.html#importlib.reload

But be aware that this library had some API-changes with the last two minor versions.

CivFan
  • 13,560
  • 9
  • 41
  • 58
funky-future
  • 3,716
  • 1
  • 30
  • 43
42

If you want to import a specific function or class from a module, you can do this:

import importlib
import sys
importlib.reload(sys.modules['my_module'])
from my_module import my_function
jss367
  • 4,759
  • 14
  • 54
  • 76
  • 1
    I've been debugging helper functions included with a module that also imports a C/Python api and therefore can only be reloaded by restarting the kernel. This makes things so much faster! – taranaki Aug 21 '21 at 03:20
15

Another small point: If you used the import some_module as sm syntax, then you have to re-load the module with its aliased name (sm in this example):

>>> import some_module as sm
...
>>> import importlib
>>> importlib.reload(some_module) # raises "NameError: name 'some_module' is not defined"
>>> importlib.reload(sm) # works
András Aszódi
  • 8,948
  • 5
  • 48
  • 51
  • 1
    Note that this is not a special rule but rather a consequence of Python modules being first-class. The `import as ` statement simply assigns `` to a module *object*. The `reload()` function then accepts this object. The `reload()` function does not care (or know) about the name. – Quelklef Apr 07 '21 at 18:32
  • 1
    @Quelklef Good point, thanks for the comment. – András Aszódi Apr 08 '21 at 12:59
1

Although the provided answers do work for a specific module, they won't reload submodules, as noted in This answer:

If a module imports objects from another module using from ... import ..., calling reload() for the other module does not redefine the objects imported from it — one way around this is to re-execute the from statement, another is to use import and qualified names (module.*name*) instead.

However, if using the __all__ variable to define the public API, it is possible to automatically reload all publicly available modules:

# Python >= 3.5
import importlib
import types


def walk_reload(module: types.ModuleType) -> None:
    if hasattr(module, "__all__"):
        for submodule_name in module.__all__:
            walk_reload(getattr(module, submodule_name))
    importlib.reload(module)


walk_reload(my_module)

The caveats noted in the previous answer are still valid though. Notably, modifying a submodule that is not part of the public API as described by the __all__ variable won't be affected by a reload using this function. Similarly, removing an element of a submodule won't be reflected by a reload.

skasch
  • 101
  • 1
  • 6
-1
import sys

del sys.modules['module_name']
import module_name
Boskosnitch
  • 774
  • 3
  • 8
Aldrin
  • 11
  • 2
  • 2
    Please add some explantion about the code, it will be much more informative and helpful :) – Reznik Mar 12 '21 at 15:12
  • Here is the explanation: If the module is previously loaded/imported then the module is added to the list of modules imported in sys.modules list and the import commands that follow are ignored. By removing the module from the sys.modules list, the next time you use the import cammand, the module is reloaded and the module name is readded to the sys.modules list. – hekimgil May 30 '22 at 23:55