[Tutor] Returning multiple values from a script

Hans Dushanthakumar Hans.Dushanthakumar at navman.com
Thu Jan 12 02:34:52 CET 2006


Oops answered my own question. Dynamic importing is done using the
__import__ function:

t = ["test1.py", "test2.py"] #Actually this list is filled in by a
Tkinter Listbox selection.

for f in t:
    testname = f[:-3]
    test = __import__("%s"%(testname))
    res = test.run_test()

Cheers
Hans


-----Original Message-----
From: Hans Dushanthakumar 
Sent: Thursday, 12 January 2006 2:28 p.m.
Cc: Python Tutor
Subject: RE: [Tutor] Returning multiple values from a script

Thanks for your reply Kent.

Is it possible to dynamically import a module?

The foll snippet of code throws an error "ImportError: No module named
testname"


t = ["test1.py", "test2.py"] #Actually this list is filled in by a
Tkinter Listbox selection.

for f in t:
    testname = f[:-3]
    import "%s"%(testname)
    print testname.run_test()


Any other means of importing dynamically?




-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of Kent Johnson
Sent: Thursday, 12 January 2006 2:02 p.m.
Cc: Python Tutor
Subject: Re: [Tutor] Returning multiple values from a script

Hans Dushanthakumar wrote:
> Yes I agree that it'd be cleaner to import the second script and call 
> it.
> 
> The reason I'm keen to find a alternate method is that I have a whole 
> lot of scripts that were designed to run only as standalone scripts.
> ie each of these scripts is not a "function" that I could just call 
> from another script. They are all of the format:
> 
> 1) Test.py
> -------
> import sys
> 
> sys.exit(5)
> 
> 
> Now I'm trying to write a master script that'd run each one of these 
> scripts. I'm sure it would have been a lot easier if the scripts were 
> of the following format. Unfortunately they are not.:
> 
> Test.py
> -------
> import sys
> 
> Def test():
>     return(5, "Junk")
> 
> if __name__ == __main__:
>    test()
> 
> 
> Well if there is no other way I think I'll have to alter all the 
> scripts to be of the above format. Just wondering if anyone has any 
> suggestions

ISTM that you have to change all the scripts anyway if you want to
return two values...why not change them to call a different function
(other than sys.exit) that does what you want? Do the scripts still have
to run standalone?

For example you could make a module mysys.py:

returnedvalue = None

def exit(value):
   global returnedvalue
   returnedvalue = value

Then just edit the scripts to import mysys and call mysys.exit(), run
the script with import and get the returnedvalue from mysys.

If you want a base hack that I couldn't possibly recommend :-) I suppose
you could replace sys.exit() with a function of your own choosing. 
Something like this should work...

  >>> import sys
  >>> returnedvalue = None
  >>>
  >>> def mysysexit(value):
  ...   global returnedvalue
  ...   returnedvalue = value
  ...
  >>> original_exit = sys.exit # if you need to keep the old value...
  >>> sys.exit = mysysexit
  >>>
  >>> sys.exit(5) # here you can just import the module you want to run
  >>> returnedvalue
5

Isn't Python wonderful!
Kent



More information about the Tutor mailing list