find out whether a module exists (without importing it)

Gelonida N gelonida at gmail.com
Mon Aug 6 18:40:49 EDT 2012


On 08/06/2012 11:58 PM, Miki Tebeka wrote:
>> imp.find_module(), but
>> it didn't find any module name containing a '.'
> The docs (http://docs.python.org/library/imp.html#imp.find_module) clearly say:
>
> "This function does not handle hierarchical module names(names
 > containing dots).
Thanks,
Well this explains.

 >  In order to find P.M, that is, submodule M of package P, use 
find_module() and load_module() to find and load package P, and then use 
find_module() with the path argument set to P.__path__. When P itself 
has a dotted name, apply this recipe recursively."
>
> See https://gist.github.com/3278829 for possible implementation.
>


Using imp and then iterating (as you suggested) is probably the fastest 
solution. This is what I will do.

Thanks again.




More information about the Python-list mailing list