Is automatic reload of a module available in Python?
Arnaud Delobelle
arnodel at googlemail.com
Wed Feb 17 09:52:32 EST 2010
"R (Chandra) Chandrasekhar" <chyavana at gmail.com> writes:
> Dear Folks,
>
> I am currently developing a python program, let us call it
> "generic.py", and I am testing out the functions therein by testing
> them out interactively in the python interpreter by invoking python
> and doing
>
> import generic
>
> Once I hit an error, I need to revise my file and reload the module using
>
> reload(generic)
>
> The difference in syntax between invoking import and reload is really
> costing me time and patience.
>
> Therefore, I would like to ask:
>
> 1. Is there a method of auto-reloading a module while developing it
> and testing it with the interactive python interpreter?
>
> 2. Is there a better way of developing a program?
>
> Thank you.
>
> Chandra
Here is a very simple way to improve what you do, which won't require
you to change the way you work or to learn a new paradigm:
Instead of testing your functions interactively, put your testing code
in a file, e.g. 'program_tests.py'. Your can then type
python program_tests.py
at the shell interactive prompt. To perform the tests again, just
re-execute that file. If your tests are divided into
different units, you can put these in functions:
def test_frobz():
#testing code for frobzation of klops
def test_frizz():
#testing code for frizzment of frobzied klops
# etc..
So if you want to keep doing interactive tests, you can import
program_tests and call whichever testing functions you want. You may
even have arguments to those functions to test them with different
parameters.
I know some people will point at more 'pro' ways of testing but this has
the merit of being very straightforward. Then when you move on to more
sophisticated techniques, I think you will understand better the
motivations behind them.
--
Arnaud
More information about the Python-list
mailing list