Re-loading updated modules

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

    #1

    Re-loading updated modules

    Hi,
    Shouldn't python recompile a module if there is a later version of the
    code (.py file)? While i am debuging, i always have to exit python and
    delete the pyc before every run, then start it again and import the
    modules. It seems that this is the only way for it to recompile the
    new code.

    What is going on? is this normal behaviour? I noticed that on both
    Windows and linux (Fedora Core 4, and 5 and RHEL 4)

  • Pierre Quentel

    #2
    Re: Re-loading updated modules

    Instead of exiting the interpreter, you can use reload() : see the
    section "Built-in functions" in the library reference

    "reload( module)
    Reload a previously imported module. The argument must be a module
    object, so it must have been successfully imported before. This is
    useful if you have edited the module source file using an external
    editor and want to try out the new version without leaving the Python
    interpreter. The return value is the module object (the same as the
    module argument). "

    Pierre

    Comment

    • Fredrik Lundh

      #3
      Re: Re-loading updated modules

      "fileexit" <mayyash@gmail. com> wrote:
      [color=blue]
      > Shouldn't python recompile a module if there is a later version of the
      > code (.py file)?[/color]

      it does, when the module is first loaded.
      [color=blue]
      > While i am debuging, i always have to exit python and
      > delete the pyc before every run[/color]

      deleting the PYC shouldn't be necessary.
      [color=blue]
      > then start it again and import the
      > modules. It seems that this is the only way for it to recompile the
      > new code.[/color]

      reload(module)

      however, see the caveats on this page:


      [color=blue]
      > What is going on? is this normal behaviour?[/color]

      yes. and this is tutorial stuff, too:



      </F>



      Comment

      • Paul McGuire

        #4
        Re: Re-loading updated modules

        "fileexit" <mayyash@gmail. com> wrote in message
        news:1151414294 .809032.44850@u 72g2000cwu.goog legroups.com...[color=blue]
        > Hi,
        > Shouldn't python recompile a module if there is a later version of the
        > code (.py file)? While i am debuging, i always have to exit python and
        > delete the pyc before every run, then start it again and import the
        > modules. It seems that this is the only way for it to recompile the
        > new code.
        >
        > What is going on? is this normal behaviour? I noticed that on both
        > Windows and linux (Fedora Core 4, and 5 and RHEL 4)
        >[/color]
        If there is a later .py file, and you restart Python, Python will recompile
        the modules when you import them.

        -- Paul



        Comment

        Working...