Re: How to re-import a function from a module?

2008-11-06 Thread Aaron Brady
On Nov 5, 7:36 pm, Kurda Yon <[EMAIL PROTECTED]> 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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to re-import a function from a module?

2008-11-06 Thread Diez B. Roggisch
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
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to re-import a function from a module?

2008-11-05 Thread Ben Finney
Kurda Yon <[EMAIL PROTECTED]> writes:

> 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
--
http://mail.python.org/mailman/listinfo/python-list


How to re-import a function from a module?

2008-11-05 Thread 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.
--
http://mail.python.org/mailman/listinfo/python-list