[Tutor] import and reload in modules

Garry Willgoose Garry.Willgoose at newcastle.edu.au
Mon Nov 5 01:17:32 CET 2007


I'm writing a simple modelling environment that provides a GUI for  
dynamically coupling fortran and C subroutines to solve a science  
problem. The GUI and environment glue are all python. One thing I  
want to do is allow the user to load a python file that provides the  
user defined details on how the C/Fortran modules are to be coupled  
in the science model ... basically a script file if you like. They  
use the GUI to select and run the python file. I have no control over  
whether/when the user edits the python script and wants to rerun the  
script without restarting the environment so I need to be able to  
(repeatedly) reload the current script at any time

I'm having problems with import and reload. Here is a simple test  
code (mytest.py) that runs from the python prompt that shows the  
problem, and the log of my session from the python command line.

In the real code the variable 'model' is passed into load() ... I've  
just set it here to make the code self contained. At the first load()  
the version in siberia900.py is set to 1.00. Between the first and  
second load() I edit siberia900.py so that version is 0.00 (just an  
easy way to see if the modified code is reloaded). Clearly the reload  
fails (and if I don't have the try/except ... that is the commented  
line is executed ... it crashes with the error 'NameError: name  
'siberia900' is not defined'). Also clearly reloading mytest also  
doesn't change anything.

Now all this runs fine (i.e. the correct version numbers are output)  
at the python interpretter line so its clearly something to do with  
it being inside the module mytest which I don't understand. Finally  
if I uncomment the bottom two lines and run it at the unix command  
line as 'python mytest.py' it still fails in the same way (i.e. the  
'except' line is executed).

Can anyone please explain what's going on ... and how I might be able  
to do what I need to do.

------------------------------------
current_model_name=''

def load():
   global current_model_name
   model='siberia900'
   text1='import '+model
   text2='reload('+model+')'
   if model==current_model_name:
     try:
       exec(text2)
       print 'here 2',text2
     except:
       print 'here 3',text1
       exec(text1)
#    exec(text2)
   else:
     print 'here 10', text1
     exec(text1)
   current_model_name=model
   return(siberia900.version)

#load()
#load()
----------------------------------------
-----------------------------------------
 >>> import mytest
 >>> mytest.load()
here 10 import siberia900
'1.00'
 >>> mytest.load()
here 3 import siberia900
'1.00'
 >>> reload(mytest)
<module 'mytest' from 'mytest.pyc'>
 >>> mytest.load()
here 10 import siberia900
'1.00'
-----------------------------------------

====================================================================
Prof Garry Willgoose,
Australian Professorial Fellow in Environmental Engineering,
Director, Centre for Climate Impact Management (C2IM),
School of Engineering, The University of Newcastle,
Callaghan, 2308
Australia.

Centre webpage: www.c3im.org.au

Phone: (International) +61 2 4921 6050 (Tues-Fri AM); +61 2 6545 9574  
(Fri PM-Mon)
FAX: (International) +61 2 4921 6991 (Uni); +61 2 6545 9574 (personal  
and Telluric)
Env. Engg. Secretary: (International) +61 2 4921 6042

email:  garry.willgoose at newcastle.edu.au;  
g.willgoose at telluricresearch.com
email-for-life: garry.willgoose at alum.mit.edu
personal webpage: www.telluricresearch.com/garry
====================================================================
"Do not go where the path may lead, go instead where there is no path  
and leave a trail"
                           Ralph Waldo Emerson
====================================================================






More information about the Tutor mailing list