[Baypiggies] how to programmatically invoke: from module import *

Tung Wai Yip tungwaiyip at yahoo.com
Wed Mar 4 20:36:20 CET 2009


I end up writing something like this

     mod = __import__(module_name,fromlist=['*'])
     imported = dict((n,v) for n, v in vars(mod).items() if not  
n.startswith('_'))

This code has not considered __all__.

The fromlist=['*'] argument in __import__() is rather surprising. It  
doesn't really execute a "from module import *" as I expect. I still have  
to do filtering manually. All it does is a subtle difference in the module  
returned. In fact I can pass fromlist=['xxx'] and the result obtained  
seems to be the same.

Wai Yip



> vars(__import__('module')) should get you a dictionary close to what
> you desire.  It may need tweaking because it does not apply the extra
> restrictions that "import *" does (__all__ and leading underscores).
>
>
> Alex
>
>
> On Tue, Mar 3, 2009 at 9:55 AM, Tung Wai Yip <tungwaiyip at yahoo.com>  
> wrote:
>> I always have some trouble in understanding the API for modules  
>> importing
>> like the __import__(). And this one I really need some help. How can I
>> programmatically invoke: from module import *? I want to put the  
>> references
>> in a dictionary and pass it some other module.
>>
>> Thank you,
>>
>> Wai Yip
>>
>> _______________________________________________
>> Baypiggies mailing list
>> Baypiggies at python.org
>> To change your subscription options or unsubscribe:
>> http://mail.python.org/mailman/listinfo/baypiggies
>>





More information about the Baypiggies mailing list