Developing and testing modules live

Tim Peters tim_one at email.msn.com
Mon Oct 18 03:26:29 EDT 1999


[Nolan Darilek]
> When I develop modules, I prefer testing them immediately after I
> write each specific function. One solution I've tried is to run an
> instance of the Python interpreter in a shell buffer alongside my
> editting in another. When I import the module, test some
> functionality and modify the source file, I've discovered that del'ing
> and re-importing the module doesn't include the changes.
> ...

That's right -- del'ing the module merely removes a local name for the
loaded module object, which latter remains intact (it's still living in the
sys.modules dict).  The builtin reload() function does what you want (see
the Library Reference Manual).

Notes:

+ If you're writing nested packages, it can get harder.

+ Use one of the Python IDEs instead of a shell.  Anything from the Emacs
python-mode to the Tk-based IDLE will make this easier (e.g., IDLE's
"import" menu entry does an import or a reload, depending on whether the
module has already been loaded; pymode has similar gimmicks in the Python
process buffer).

+ See doctest.py in python.org's FTP contrib area for a painless way to save
those interactive testing sessions as forever-after auto-verified examples.

life-is-beautiful-ly y'rs  - tim






More information about the Python-list mailing list