Re-loading updated modules

Discussion in 'Python' started by fileexit, Jun 27, 2006.

  1. fileexit

    fileexit Guest

    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)
    fileexit, Jun 27, 2006
    1. Advertisements

  2. 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 Quentel, Jun 27, 2006
    1. Advertisements

  3. it does, when the module is first loaded.
    deleting the PYC shouldn't be necessary.

    however, see the caveats on this page:
    yes. and this is tutorial stuff, too:

    Fredrik Lundh, Jun 27, 2006
  4. fileexit

    Paul McGuire Guest

    If there is a later .py file, and you restart Python, Python will recompile
    the modules when you import them.

    -- Paul
    Paul McGuire, Jun 27, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.