How to re-import a function from a module?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kurda Yon

    How to re-import a function from a module?

    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.
  • Ben Finney

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

    Kurda Yon <kurdayon@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

    Comment

    • Diez B. Roggisch

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

      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

      Comment

      • Aaron Brady

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

        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 InteractiveCons ole class. When you make a change, run the
        command:
        >>changed
        Your subclass of InteractiveCons ole 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.

        Comment

        Working...