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.

Essentially what you need is

def import_or_reload():
 """assume we want to load or reload sys"""
 if 'sys' in dir():
  reload(sys)
else: 
  import sys

but this runs into the problem that sys is imported within the local scope of 
the function, so you insert a global statement



def import_or_reload2():

 """Add 'global sys'"""
 global sys

 if 'sys' in dir():

  reload(sys)

else: 

  import sys

'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





def import_or_reload3():


 """Add 'global modules'"""

 global sys
 global modules



 if 'sys' in modules:


  reload(sys)


else: 


  import sys
  modules.append('sys')

now lets add a parameter to the function signature, so any module name can be 
passed as an argument and loaded





def import_or_reload4(module_name):



 """Add exec"""
 

exec 'global %s'%module_name

 global modules





 if module_name in modules:



  exec 'reload(%s)'%module_name



else: 



  exec 'import %s'%module_name

  exec 'modules.append(\'%s\')'%module_name

but this doesn't work as global does not cooperate with exec
is there a __reload__('X') function like there is an __import__(‘X’) function? 

Also is there a better way to import modules at run time?

Cheers and here is the test script in case you can't access the attachment

def a():
    global modules
    global sys
    import sys

    modules.append('sys')

def b():
    global modules
    global sys

    reload(sys)

def c(module_name):
    global modules
    exec 'global %s'%module_name
    exec 'import %s'%module_name

    modules.append(module_name)

def test():
    global modules
    global sys

    #create the module list to contain all the modules
    modules=[]

    print 'originally dir() returns:'
    print dir()

    a()
    print 'function a() properly imports the following module:'
    print sys
    print 'is %s in %s->%s'%('sys',modules,'sys' in modules)
    
    b()
    print 'function b() properly reloads the following module:'
    print sys
    print 'is %s still in %s->%s'%('sys',modules,'sys' in modules)

    try:
        c('os')
        print 'function c() properly imports the following module:'
    except:
        print 'function c() failed to import module os'
        print 'is %s in %s->%s'%('os',modules,'os' in modules)

    try:
        print os
        print 'is %s still in %s->%s'%('os',modules,'os' in modules)
    except:
        print 'os was loaded, but is not visible outside of the scope of c()'
--- On Fri, 3/19/10, python-list-requ...@python.org 
<python-list-requ...@python.org> wrote:

From: python-list-requ...@python.org <python-list-requ...@python.org>
Subject: Python-list Digest, Vol 78, Issue 192
To: python-list@python.org
Received: Friday, March 19, 2010, 7:05 AM

Send Python-list mailing list submissions to
    python-list@python.org

To subscribe or unsubscribe via the World Wide Web, visit
    http://mail.python.org/mailman/listinfo/python-list
or, via email, send a message with subject or body 'help' to
    python-list-requ...@python.org

You can reach the person managing the list at
    python-list-ow...@python.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Python-list digest..."
Today's Topics:

   1. Re: import antigravity (John Bokma)
   2. Re: Truoble With A Search Script for ImageBin.org
      (Cameron Simpson)
   3. example of ssl with SimpleXMLRPCServer in 2.6? (Rowland Smith)
   4. Re: multiprocessing on freebsd (Tim Arnold)
   5. Re: Python bindings tutorial (Tim Roberts)
   6. Re: sqlite3, memory db and multithreading (John Nagle)
   7. Re: GC is very expensive: am I doing something wrong?
      (Patrick Maupin)
   8. Bug in Python APscheduler module. (anand jeyahar)
   9. Re: Bug in Python APscheduler module. (Terry Reedy)
  10. Re: Python bindings tutorial (Alf P. Steinbach)
-- 
http://mail.python.org/mailman/listinfo/python-list


      __________________________________________________________________
Looking for the perfect gift? Give the gift of Flickr! 

http://www.flickr.com/gift/
def a():
	global modules
	global sys
	import sys

	modules.append('sys')

def b():
	global modules
	global sys

	reload(sys)

def c(module_name):
	global modules
	exec 'global %s'%module_name
	exec 'import %s'%module_name

	modules.append(module_name)

def test():
	global modules
	global sys

	#create the module list to contain all the modules
	modules=[]

	print 'originally dir() returns:'
	print dir()

	a()
	print 'function a() properly imports the following module:'
	print sys
	print 'is %s in %s->%s'%('sys',modules,'sys' in modules)
	
	b()
	print 'function b() properly reloads the following module:'
	print sys
	print 'is %s still in %s->%s'%('sys',modules,'sys' in modules)

	try:
		c('os')
		print 'function c() properly imports the following module:'
	except:
		print 'function c() failed to import module os'
		print 'is %s in %s->%s'%('os',modules,'os' in modules)

	try:
		print os
		print 'is %s still in %s->%s'%('os',modules,'os' in modules)
	except:
		print 'os was loaded, but is not visible outside of the scope of c()'
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to