How to call script in interative status

Steven Taschuk staschuk at telusplanet.net
Fri Feb 28 17:59:51 CET 2003


Quoth Fu Chen:
> yes, i write the script like below
> 
> #######################################################
> def main():
> 	import getopt
> 	try:
> 		opts, args = getopt.getopt(sys.argv[1:], "slc:d:e:")
  [...]
> then in the command line >>>, how can i run it with args?
> import script
> script.main()
> but where is the args to input?
  [...]
> I know i can press ctrl-R in pythonwin to invoke a dialog to enter the
> args, but i hate it if i have to run it several times.

I'm not sure I understand what you want.

If you just want to run the script, why not run it from the
command line?

If you want to run it from other code (and in the interpreter),
then it's best to give it a good programmatic interface and use
that.  Let the __name__=='__main__' block be just a trivial use of
the programmatic interface.  In particular, don't use sys.argv
anywhere except in that block.  For a trivial example:
	def do_something(arg_array):
		... spin through arg_array extracting options
		... do whatever the options say
	if __name__ == '__main__':
		do_something(sys.argv)
Then you can do
	>>> import script
	>>> script.do_something(['foo', 'bar'])
or whatever.

The quick-and-dirty method is:
	>>> import sys
	>>> sys.argv = ['foo', 'bar']
	>>> execfile('script.py')

-- 
Steven Taschuk                                     staschuk at telusplanet.net
Receive them ignorant; dispatch them confused.  (Weschler's Teaching Motto)





More information about the Python-list mailing list