"J. Cliff Dyer" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > bambam wrote: >> import works in the main section of the module, but does >> not work as I hoped when run inside a function. >> >> That is, the modules import correctly, but are not visible to >> the enclosing (global) scope. >> >> Questions: >> (1) Where can I read an explanation of this? >> (2) Is there a work around? >> >> BTW, sys.modules("filename") shows that the module is >> loaded, I just don't know how to use it when loaded that >> way. Also, if I import again at the global scope, the module >> name becomes available. >> >> Steve. >> >> --- >> >>>>> def gim(): >>>>> >> ... exec "import gamel" >> ... >> > All you have done in this function is bind the module to the name gamel > within the scope of the function. As soon as the function exits, the > module goes out of scope. If you want to use it externally, return the > module. > > def: gim(): > import gamel > return gamel >>>>> gim() >>>>> > This will have to change to > > gamel = gim() > > and the rest should work as expected. >>>>> sys.modules["gamel"] >>>>> >> <module 'gamel' from 'c:\gamel.pyc'> >> >>>>> gamel >>>>> >> NameError: name 'gamel' is not defined >> >>>>> exec "import gamel" >>>>> gamel >>>>> >> <module 'gamel' from 'c:\gamel.pyc'> >> >> >> >
def: gim(): import gamel return gamel Unfortunately, it needs to do dynamic import: I can't list all of the possible import modules because they are unknown until runtime. Steve. -- http://mail.python.org/mailman/listinfo/python-list