How to re-import a function from a module?

Aaron Brady castironpi at gmail.com
Thu Nov 6 15:48:30 EST 2008


On Nov 5, 7:36 pm, Kurda Yon <kurda... at yahoo.com> wrote:
> Hi,
>
> I have the following small problem. I run Python interactively. In the
> beginning of the run I import many functions from many modules. Than I
> execute some commands and notice that one of the imported functions
> contains a mistake. I open another terminal in which I open the file
> with the problematic function and correct the function. However, the
> Python does not see my changes. It still uses the old version of the
> function. In principle I could close the Python session and reopen it
> again and import all functions agane. But it does not seem to be a
> convenient solution. Is there a way to force Python to re-import the
> function, i.e. to force it to use the new version of the function?
>
> Thank you in advance.

Here is another option.

Look at the InteractiveConsole class.  When you make a change, run the
command:

>>> changed

Your subclass of InteractiveConsole catches it and does not send it to
the compiler.  Instead, it closes and reruns the entire session so
far.  Or, just the imports and definitions, which you have to detect
by hand.  This is probably the hard way.



More information about the Python-list mailing list