How to re-import a function from a module?

Diez B. Roggisch deets at nospam.web.de
Thu Nov 6 11:49:09 CET 2008


Kurda Yon 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?

You can use reload, as Ben explained. Be aware though that this might
introduce subtle bugs. 

I personally prefer to write small test-scripts & simply execute them. If
you absolutely need to go interactive, you might consider using

python -i script.py

to drop to the prompt after the script has been executed.

Diez



More information about the Python-list mailing list