stop script w/o exiting interpreter

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Fri Jan 26 03:50:14 EST 2007


Alan Isaac a écrit :
> I'm fairly new to Python and I've lately been running a script at
> the interpreter while working on it.  Sometimes I only want to
> run the first quarter or half etc.  What is the "good" way to do this?

If the point is to debug your script, then import pdb; pdb.set_trace()

> Possible ugly hacks include:
> 
> - stick an undefined name at the desired stop point
> - comment out the last half
> 
> I do not like these and assume that I have overlooked the obvious.

If you have much of your code in functions/classes etc, and the bare 
minimum[1] at the top level, then you can launch a Python shell, import 
your module, and test functions as you wish...

[1]:

import XXX
import YYY

# lots of functions/classes etc

def main(argv):
   # what would have been at the top level

if __name__ == __main__:
   import sys
   sys.exit(main(sys.argv))



More information about the Python-list mailing list