How to re-import a function from a module?

K

Kurda Yon

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.
 
D

Diez B. Roggisch

Kurda said:
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
 
A

Aaron Brady

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:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,229
Latest member
GloryAngul

Latest Threads

Top