0

In my main module, I have the following:

function_dictionary = utilities.load_modules()

Which calls this:

def load_modules():

    function_dictionary = {}

    print "\nLoading modules..."
    for path, subdirs, files in os.walk("modules"):

        for name in files:

            if name.lower().endswith('.py') and name != "__init__.py":

                print "Importing " + os.path.join(path, name) + "..."

                try:
                    module = imp.load_source('utilities',os.path.join(path, name))
                    function_dictionary = dict(function_dictionary.items() + module.function_dictionary.items())

                except Exception, e:
                  print( "Import Error: %s" % str(e) )

    return function_dictionary

Now that the modules have been loaded, my main module calls this:

recovery.start("recovery.txt")

Which in turn calls this from the recovery module:

    if recovery:
        for command in content:
            print ("\n"+command)
            result = eval(command)

            if not result:
                continue_recovery = utilities.query_yes_no("Warning: Previous recovery command was failure, continue recovery?")
                if not continue_recovery:
                    break
    end()

I want eval to execute:

user_operations.remove_user(URL,acc_531,user_29080,"id",headers)

Where user_operations is the name of one of the imported modules and remove_user is a function within it.

However, I get the following error:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensio
ns\Microsoft\Python Tools for Visual Studio\2.0\visualstudio_py_util.py", line 7
6, in exec_file
    exec(code_obj, global_variables)
  File "C:\Users\mryan\Documents\Code\pcli_front.py", line
 67, in <module>
    recovery.start_recovery(recovery_file, auto_recovery, headers)
  File "C:\Users\mryan\Documents\Code\recovery.py", line 1
21, in start_recovery
    result = eval(command)
  File "<string>", line 1, in <module>
NameError: name 'user_operations' is not defined

Is this because the imported modules are not within the scope of the recovery module? If so, does anybody know how to make it so they are?

Thanks

P.s I am aware Eval is evil etc etc

TomSelleck
  • 6,706
  • 22
  • 82
  • 151
  • Why are you doing this?! What's wrong with regular `import`? – Thomas Orozco Feb 03 '14 at 16:10
  • The modules are loaded dynamically so I have no idea what is and is not there. – TomSelleck Feb 03 '14 at 16:13
  • [This question](http://stackoverflow.com/questions/3312803/from-import-with-import-function) has the answer you're looking for (the second one). However, it looks like you have coded yourself into a corner (or someone else has), and this is probably not the *right* solution to get out of it. – Thomas Orozco Feb 03 '14 at 16:19

0 Answers0