By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

How to re-import a function from a module?

P: n/a
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.
Nov 6 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Kurda Yon <ku******@yahoo.comwrites:
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?
A Python ‘import’ is conceptually two steps: execute the module, then
bind the objects that were created to names in a namespace.

The import mechanism has a short-cut: if the module has already been
imported by this Python VM, the module isn't executed again, and the
existing objects are simply re-used with new bindings as necessary.

What you want is to specifically request the module to be re-executed
to re-create the objects again, and re-bind the existing name bindings
to the objects that result.
>>help(reload)
--
\ “I hope that after I die, people will say of me: ‘That guy sure |
`\ owed me a lot of money’.” —Jack Handey |
_o__) |
Ben Finney
Nov 6 '08 #2

P: n/a
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
Nov 6 '08 #3

P: n/a
On Nov 5, 7:36*pm, Kurda Yon <kurda...@yahoo.comwrote:
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.
Nov 6 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.