Importing v reloading modules modules

Importing v reloading modules modules

Post by Peter Peym » Sun, 21 Mar 2010 02:09:09


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
 
 
 

Importing v reloading modules modules

Post by Terry Reed » Sun, 21 Mar 2010 03:02:02

Reload is effectively deprecated in 2.x and removed in 3.x, so I
recommend not using it.

Deleting an entry from sys.modules should force recreation with an
import. Just make sure all other references are also gone so that the
module object can be deleted.

The following shows how to import by name in string and globally from
within a function.

def f():
global itertools
itertools = __import__('itertools')

f()
print(itertools) # 3.1

#<module 'itertools' (built-in)>

Terry Jan Reedy

 
 
 

Importing v reloading modules modules

Post by Gabriel Ge » Sun, 21 Mar 2010 10:28:12

En Fri, 19 Mar 2010 14:09:09 -0300, Peter Peyman Puk
< XXXX@XXXXX.COM > escribi
>> 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()

Terry Reedy already gave you an answer for this import problem.
I'd like to go one step back and question whether using import/modules is
the right thing here.
Clearly A.py is not a module but a script (that's the word you used) - and
one does not *import* a script but *executes* it.
That is, instead of using import/__import__, use exec (or execfile) within
a known namespace:

import __builtin__
ns = {'__builtins__': __builtin__.__dict__}
exec contents_of_textview in ns
# any change made to ns is visible here

--
Gabriel Genellina