<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">I want to write a function which imports modules the first time, and reloads them afterwards, but I am running into problems with global variables and exec. I will include a full script, but let me elaborate first.<br><br>Essentially what you need is<br><br>def import_or_reload():<br> """assume we want to load or reload sys"""<br> if 'sys' in dir():<br>  reload(sys)<br>else: <br>  import sys<br><br>but this runs into the problem that sys is imported within the local scope of the function, so you insert a global statement<br>
<br>
def import_or_reload2():<br>
 """Add 'global sys'"""<br> global sys<br>
 if 'sys' in dir():<br>
  reload(sys)<br>
else: <br>
  import sys<br><br>'sys' is still not in dir() as dir() pertains to the local scope of the function, but one can get around this by creating a local modules list and adding the imported modules to it<br>

<br>

def import_or_reload3():<br>

 """Add 'global modules'"""<br>
 global sys<br> global modules<br><br>

 if 'sys' in modules:<br>

  reload(sys)<br>

else: <br>

  import sys<br>  modules.append('sys')<br><br>now lets add a parameter to the function signature, so any module name can be passed as an argument and loaded<br>

<br>

def import_or_reload4(module_name):<br>


 """Add exec"""<br> 

exec 'global %s'%module_name<br>
 global modules<br>
<br>


 if module_name in modules:<br>


  exec 'reload(%s)'%module_name<br>


else: <br>


  exec 'import %s'%module_name<br>
  exec 'modules.append(\'%s\')'%module_name<br><br>but this doesn't work as global does not cooperate with exec<br>is there a __reload__('X') function like there is an __import__(‘X’) function? <br><br>Also is there a better way to import modules at run time?<br><br>Cheers and here is the test script in case you can't access the attachment<br><br>def a():<br>    global modules<br>    global sys<br>    import sys<br><br>    modules.append('sys')<br><br>def b():<br>    global modules<br>    global sys<br><br>    reload(sys)<br><br>def c(module_name):<br>    global modules<br>    exec 'global %s'%module_name<br>    exec 'import %s'%module_name<br><br>    modules.append(module_name)<br><br>def test():<br>    global modules<br>    global
 sys<br><br>    #create the module list to contain all the modules<br>    modules=[]<br><br>    print 'originally dir() returns:'<br>    print dir()<br><br>    a()<br>    print 'function a() properly imports the following module:'<br>    print sys<br>    print 'is %s in %s->%s'%('sys',modules,'sys' in modules)<br>    <br>    b()<br>    print 'function b() properly reloads the following module:'<br>    print sys<br>    print 'is %s still in %s->%s'%('sys',modules,'sys' in modules)<br><br>    try:<br>        c('os')<br>        print 'function c() properly imports the following module:'<br>    except:<br>        print 'function c() failed to
 import module os'<br>        print 'is %s in %s->%s'%('os',modules,'os' in modules)<br><br>    try:<br>        print os<br>        print 'is %s still in %s->%s'%('os',modules,'os' in modules)<br>    except:<br>        print 'os was loaded, but is not visible outside of the scope of c()'<br>--- On <b>Fri, 3/19/10, python-list-request@python.org <i><python-list-request@python.org></i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: python-list-request@python.org <python-list-request@python.org><br>Subject: Python-list Digest, Vol 78, Issue 192<br>To: python-list@python.org<br>Received: Friday, March 19, 2010, 7:05 AM<br><br><div class="plainMail">Send Python-list mailing list submissions to<br>    <a
 ymailto="mailto:python-list@python.org" href="/mc/compose?to=python-list@python.org">python-list@python.org</a><br><br>To subscribe or unsubscribe via the World Wide Web, visit<br>    <a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>or, via email, send a message with subject or body 'help' to<br>    <a ymailto="mailto:python-list-request@python.org" href="/mc/compose?to=python-list-request@python.org">python-list-request@python.org</a><br><br>You can reach the person managing the list at<br>    <a ymailto="mailto:python-list-owner@python.org" href="/mc/compose?to=python-list-owner@python.org">python-list-owner@python.org</a><br><br>When replying, please edit your Subject line so it is more specific<br>than "Re: Contents of Python-list digest..."<br></div><div class="plainMail">Today's Topics:<br><br>   1.
 Re: import antigravity (John Bokma)<br>   2. Re: Truoble With A Search Script for ImageBin.org<br>      (Cameron Simpson)<br>   3. example of ssl with SimpleXMLRPCServer in 2.6? (Rowland Smith)<br>   4. Re: multiprocessing on freebsd (Tim Arnold)<br>   5. Re: Python bindings tutorial (Tim Roberts)<br>   6. Re: sqlite3, memory db and multithreading (John Nagle)<br>   7. Re: GC is very expensive: am I doing something wrong?<br>      (Patrick Maupin)<br>   8. Bug in Python APscheduler module. (anand jeyahar)<br>   9. Re: Bug in Python APscheduler module. (Terry Reedy)<br>  10. Re: Python bindings tutorial (Alf P. Steinbach)<br></div><div class="plainMail">-- <br><a href="http://mail.python.org/mailman/listinfo/python-list"
 target="_blank">http://mail.python.org/mailman/listinfo/python-list</a></div></blockquote></td></tr></table><br>
      <hr size=1>
Looking for the perfect gift?<a href="http://www.flickr.com/gift/"><b> Give the gift of Flickr!</b></a>