Re: Really newbie help

Guido.van.Rossum@cwi.nl
Wed, 01 Mar 1995 00:12:24 +0100

> I've been writing python for a few months by the crude expedient of
> creating an executable <name>.py file and executing it. I then check
> the error message, modify the code, and do it again. I know this is not
> optimal. This is after all an interpreter. There must be a better way.
>
> In the documentation I see lots of documentation of specific python
> features but no hints as to how an interactive debugging session
> should go. I can enter code to be executed but I have to retype it
> into a file after it is debugged. There must be something in between.

If you have Emacs, the Python Emacs mode (or is that the Emacs Python
mode? :-) has a few commands that make it really easy to try out
pieces of Python -- ^C-^C executes the current buffer, either in a
fresh Python interpreter or in an interactive Python interpreter in
another window/buffer that you have started before with ^C-!.

Without Emacs, e.g. on the Mac, you can often get by using reload() or
execfile(). Reload() takes a module object that has already
successfully been imported and re-parses and then re-executes the
module. I often structure my modules so that all the essential code
is in a function test(). I can then do the following:

>>> import spam
>>> spam.test() # first test
(goes blatantly wrong; edit the source in another window)
>>> reload(spam)
>>> spam.test() # second test
(etc.)

Note that if the first "import spam" statement fails, there are two
possibilities: if it's a syntax error in spam, you should fix the
error and import again; if it's a run-time error in spam, you should
fix the error, and then:

>>> import spam # doesn't re-read the file
>>> reload(spam) # re-read the file
>>> spam.test()

Hope this helps,

--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>