25

After import functions into ipython, how do I reload them when I have modified them outside of ipython ?

Frankie Ribery
  • 11,933
  • 14
  • 50
  • 64

3 Answers3

32

Python 2:

reload(module)

Python 3:

from importlib import reload
reload(module)

Where module is the file with your functions.

Community
  • 1
  • 1
JBernardo
  • 32,262
  • 10
  • 90
  • 115
  • 5
    This is correct. However, reload() does not automatically recurse to imports-of-imports, so if you modify something that is imported indirectly, it won't get reloaded. – Amber Jun 21 '11 at 04:47
  • 1
    @Amber Yes, that's a limitation. You can do `x = module.x` or a function which does that for you if you need to rebind those names – JBernardo Jun 21 '11 at 04:52
  • 2
    Or you can just `reload(sys.modules['foo_module'])` to reload `foo_module`. – Amber Jun 21 '11 at 05:07
  • 7
    @Amber: IPython actually offers a `dreload` function which works recursively. – Thomas K Jun 21 '11 at 12:25
  • 1
    python3 need to do `from importlib import reload` first – wordsforthewise Jul 15 '18 at 02:44
6

you can also use autoreload, so that the modules you are working on are automatically reloaded at each statement, pretty handy for debugging, see:

Autoreload of modules in IPython

Community
  • 1
  • 1
Andrea Zonca
  • 8,378
  • 9
  • 42
  • 70
3

Use the following link to read more about reload built-in function. Please find sample below:

import controls.grid
reload(controls.grid)

Note that reload is 'Reload a previously imported module. The argument must be a module object, so it must have been successfully imported before.' and '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. If the new version of a module does not define a name that was defined by the old version, the old definition remains.'

Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52