[Pythonmac-SIG] MacPython IDE doesn't reload edited files

Just van Rossum just@letterror.com
Mon, 8 Mar 1999 11:53:08 +0100

At 8:30 PM -0800 3/7/99, Joseph J. Strout wrote:
>I find that the IDE will reload your module if you Run it again.  (It
>doesn't matter whether you save it or not.)  This is obvious if you have a
>single-module project, less obvious for more complex projects.  Suppose
>your main program is in A.py, which calls stuff in B.py.  Simply editing
>B.py and saving is not enough; running A.py will run the old B, since it
>still has a reference to the old B data in its namespace.  You have to run
>B (even if running B has no apparent effect, because it has no main
>program), and then run A.

You can save yourself a lot of trouble if you try to avoid from module
import * imports.

Usually, if module A.py does "import B", editing and running module B does
what you would expect. However, if module A defines a class which
subclasses from a class from B, you will need to run (or reload) A as well;
otherwise the class in A would still reference the old superclass.

In general, running a module is the same as reloading a module. So there
are three options:
- open the module and choose "Run"
- select the module in the module browser and choose "Reload"
- use the reload() function.

I unually find the first option most convenient.

>In very complex projects, where lots of modules have lots of references to
>other modules, it can difficult to force all the different modules to
>forget their references to old code.  I have one project like this, where
>the easiest thing to do is to quit and restart the IDE.  I hate that too,
>but I don't know what to do about it -- reloading modules has always been a
>pain, even in traditional console-mode Python.

There is one feature of the IDE which can help an awful lot in complex
situations: you can run *parts* of classes. Just select a single method (or
more) and choose "Run selection". This will update that section without
creating a new class, so all existing references (even "live" objects!)
will still be ok. Very powerful. I use it all the time when working on the
IDE itself: I hardly ever have to restart it.
Two gotchas:
- if the method is stored as a callback somewhere, that callback will *not*
be updated (it still references the old code)
- linenumbers as stored in code objects may not be valid anymore after you
edit and run preceding code. This may cause tracebacks that appear to lie.