Big problem fetching members from dynamically loaded module

Philippe C. Martin philippe at philippecmartin.com
Sun Jun 26 16:56:21 CEST 2005


> My French is mostly read-only, so let me rephrase:
Thank you :-)

> 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.
What would you suggest then, I just want my code to
1) be sturdy
2) some of the modules might not be there depending on the configuration,
and  I want their members in the menus if they are installed.

If there is a cleaner way, I'm ready to try it.


> 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)

That was it, thank you!

> 
> 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)
> 
I will study that.


Many thanks

Philippe



Peter Otten wrote:

> 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