Stefano Borini
Stefano Borini

Reputation: 143905

Reimport a module while interactive

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

Upvotes: 597

Views: 339432

Answers (8)

dstone42
dstone42

Reputation: 11

If you're already in interactive and have waited for long-running code, and you need to reload something you imported using from mod import func and it would take a long time to re-run all the previous cells, this is the solution I found:

import importlib
import mod

importlib.reload(mod)

func = mod.func

This will reassign the previously loaded function with the new version and not require you to change your function calls to include the module name. Thereafter, you should just be able to reload the module again and reassign the function every time you make edits.

Upvotes: 0

Benjamin Wohlwend
Benjamin Wohlwend

Reputation: 31868

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.

Upvotes: 596

Aldrin
Aldrin

Reputation: 11

import sys

del sys.modules['module_name']
import module_name

Upvotes: -1

Andr&#225;s Asz&#243;di
Andr&#225;s Asz&#243;di

Reputation: 9690

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

Upvotes: 17

jss367
jss367

Reputation: 5401

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

Upvotes: 51

skasch
skasch

Reputation: 101

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.

Upvotes: 1

Andrew
Andrew

Reputation: 4338

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

Upvotes: 356

funky-future
funky-future

Reputation: 3978

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.

Upvotes: 46

Related Questions