Big problem fetching members from dynamically loaded module

Peter Otten __peter__ at web.de
Sat Jun 25 16:55:42 EDT 2005


Philippe C. Martin wrote:

> OK Peter, first of all thanks.
> 
> You seem to be German and although I leave in the states, I'm French and
> your english is clearly far more advanced than mine: I have yet to
> understand a few of your comments ;-)

My French is mostly read-only, so let me rephrase: 

from module import *, try ... except, eval(), exec all work together to make
your program harder to understand and more likely to fail in obscure ways.

>> Care to provide the traceback?
> 
> Traceback (most recent call last):
>   File "SC_Shell.py", line 1095, in ?
>     l_d = SC_Shell()
>   File "SC_Shell.py", line 326, in __init__
>     self.__Make_Menu_Area()
>   File "SC_Shell.py", line 828, in __Make_Menu_Area
>     l = inspect.getmembers(eval(c))
>   File "<string>", line 0, in ?
> NameError: name 'BC' is not defined

That traceback and the code you posted do not fit together. So back to
square one.

Staring at the code you posted, I think one thing you could try would be to
modify the eval() to

eval("BC", self.m_process_global, self.m_process_local)

to ensure that eval() and exec share the same namespace.
Generally speaking, I would prefer (I hope I got that right)

module = __import__("package.module", globals(), locals(), ["module"])
members = inspect.getmembers(module)

Members of the module would then be accessed via getattr():

member = getattr(module, member_name)

Peter




More information about the Python-list mailing list