Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > find out whether a module exists (without importing it)

Reply
Thread Tools

find out whether a module exists (without importing it)

 
 
Gelonida N
Guest
Posts: n/a
 
      08-06-2012
Is this possible.

let's say I'd like to know whether I could import the module
'mypackage.mymodule', meaning,
whther this module is located somewhere in sys.path

i tried to use

imp.find_module(), but
it didn't find any module name containing a '.'

Am I doing anything wrong?

Is there another existing implementation, that helps.

I could do this manually, but this is something I'd just like to do if
necessary.


 
Reply With Quote
 
 
 
 
Miki Tebeka
Guest
Posts: n/a
 
      08-06-2012
> 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). 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.
 
Reply With Quote
 
 
 
 
Miki Tebeka
Guest
Posts: n/a
 
      08-06-2012
> 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). 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.
 
Reply With Quote
 
Gelonida N
Guest
Posts: n/a
 
      08-06-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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: find out whether a module exists (without importing it) Chris Angelico Python 0 08-07-2012 09:08 AM
Re: find out whether a module exists (without importing it) Peter Otten Python 0 08-07-2012 08:27 AM
Re: find out whether a module exists (without importing it) Gelonida N Python 0 08-07-2012 08:00 AM
Re: find out whether a module exists (without importing it) Michael Poeltl Python 0 08-07-2012 06:43 AM
How to keep a module with the same name as a module it is importing from importing itself? plb Python 2 02-08-2005 03:14 PM



Advertisments