Search code examples
pythonreload

Python reload with dynamic import (imp.load_source)


I am using python2.5. I need to import a module dynamically and then reload on change. How do I achieve this.

I tried as in below example - does not work:

import imp
modfile = 'mymod_info.py'
modname = 'mymod'
modhandle = imp.load_source (modname, modfile)
reload (modhandle)

Static import and reload works (I am not looking for this):

import mymod_info as mymod
reload (mymod)

Thanks in advance


Solution

  • If the module was already initialized, imp.load_source will initialize the module again. So instead of reload, just call

    modhandle = imp.load_source(modname, modfile)