[Zope] Refreshing modules imported into external methods

Michael Hartl michael_hartl at yahoo.com
Sat Dec 20 12:46:52 EST 2003


--- Dieter Maurer <dieter at handshake.de> wrote:
> >This worked.  It seems strange from a Python perspective, since there's no
> >need to use "reload" inside ordinary programs; Python automatically
> >re-imports any code that has changed.
> 
> In fact, Python does not care at all when you change a module
> after it has been imported. Especially, it does *not* reload
> modules in ordinary programs.
> 
> When you see automatic reloading then this is a feature of something
> outside Python (e.g. Zope or the IDE).

Perhaps you mean that the Python interpreter doesn't automatically reload
modified modules when run interactively; I certainly agree with that.  On the
other hand, when a module is included in another program, any changes to the
module are automatically reflected in the program when it is run at the Linux
command line.  (Essentially the same thing happens under Windows when a
Python file is run as a script by double-clicking on it.)

I'm sure Dieter already knows about this, but let me elaborate for the sake
of those on the list without extensive Python experience.  Consider the
following two files:

foo.py:

import bar
print bar.baz()


bar.py:

def baz():
    return 'This is an unmodified module.'

    
Running foo.py at the command line produces the following:

$ python foo.py
This is an unmodified module.

Running foo.py also causes the creation of a bytecode version of bar.py
(namely, bar.pyc).  If we now edit bar.py so that it reads 

def baz():
    return 'This is a modified module.'

and then run foo.py at the command line again, we have

$ python foo.py
This is a modified module.

Here Python recognizes that bar.py is newer than its bytecode counterpart
bar.pyc, so it produces a new bar.pyc bytecode file, and then uses the
modified version of bar.baz() inside foo.py.  In other words, even though we
have not explicitly reloaded bar.py (with the reload(bar) command), its
changes nevertheless show up when we run foo.py.  Moreover, if bar.py itself
imports a module called quux, then any changes to quux.py will propagate all
the way up to foo.py, and so on.  The result is that at no point does the
programmer need to pay attention to the modification status of any imported
modules; Python handles all of it for you.

Because of the behavior of Python programs in the file system, I expected the
same behavior for external methods called from Zope, since external methods
live in files on the file system.  This expectation was not met; an explicit
call to reload() is necessary for any changes in external modules to be
reflected in the methods that import them.

Michael

=====
Michael Hartl
http://www.michaelhartl.com/

__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/



More information about the Zope mailing list