[Tutor] Calling another script
Alan Gauld
alan.gauld at btinternet.com
Mon Apr 4 10:14:27 CEST 2011
"tee chwee liong" <tcl76 at hotmail.com> wrote
> i want one.py to read a configuration file and
> executes two.py and three.py.
>
> if int(operation)== 0:
> import two
> else:
> print "Default"
>
> two.py:
> print "executing script number 2"
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
importing a module is not intended as a mechanism to execute
code, that should be considered a side-effect or an initialisation
feature.
importing makes code available for use.
Put the code in the modules into functions, then import all the
modules at
the start of your program. Then execute the functions within your
if/else logic.
import two,three
operation = read_from_config_file(filename)
if operation == 0:
two.doTwo()
three.doThree()
else:
print "invalid operation"
That wil, be a more reliable and flexible approach.
In fact you could put all the operations in one module...
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list