Importing v reloading modules modules



Hello

I submitted this earlier today, but I was not clear enough, so I am posting it again.

I am running a simulator written in python. The simulator has a small TextView (actually a SourceView) widget which lets the user writes scripts, and when they are satisfied they can execute that script to get results. For arguments sake, we write a simple script and save it as A.py and we import it and execute it more or less like so.

import A

#assume there is a function called test() in module A
A.test()


Then the user modifies the contents of A.py and saves it again (to A.py) now all we have to do is the following

if 'A' in dir():
reload(A)
else:
import A

A.test()

But since the user chooses the file name, and not me, the programmer, the module names will vary. Let's assume the module names are loaded and stored in the list module_names, and we iterate over them, and pass them as arguments to a function to import or reload each model as appropriate

def import_or_reload(module_name):

if module_name in sys.modules:
#somehow reload
else:
#somehow import

does anyone know how to deal with the reload and import as they both present problems since module_name is a string, and to reload something along the lines of the below must be executed

exec 'reload(%s)'%module_name

and then we also have to deal with the scope issue since the loaded module will be local and not global. I can execute something like so

exec 'global %s'%module_name

but that does not work very well with exec

any suggestions?

Cheers


Peyman

.



Relevant Pages

  • Re: Executing other TCL - Scripts in the background
    ... > regularly when I execute it in normal executable Unix - Scripts. ... % exec ls NONEXT ... error message from the VTK script. ... Reaffirming my suspicion that it's the environment the VTK script is ...
    (comp.lang.tcl)
  • Re: Logging sftp file transfers
    ... *> just curious why you would need execute on files for ssi? ... *> requirement for ssi to work. ... (except of course, dirs need exec) ... like run a script via cron over the web dir's and look for new ...
    (RedHat)
  • Re: Exec() function not doing as I want (OSX)
    ... I have a little problem with one function within my cd-management script. ... execute with exec: ... The executable is in the same directory as the php script is in. ... exec() on it to see if the execis working right, ...
    (comp.lang.php)
  • Re: Exec() function not doing as I want (OSX)
    ... I have a little problem with one function within my cd-management script. ... execute with exec: ... The executable is in the same directory as the php script is in. ... exec() on it to see if the execis working right, ...
    (comp.lang.php)
  • Re: Script Header
    ... > On a shell script, ... > You can TRY to execute a garbage file. ... > filename: Exec format error. ... - The shell will check whether this file has the executable flag set, ...
    (comp.unix.shell)