find out whether a module exists (without importing it)

Discussion in 'Python' started by Gelonida N, Aug 6, 2012.

  1. Gelonida N

    Gelonida N Guest

    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.
    Gelonida N, Aug 6, 2012
    #1
    1. Advertising

  2. Gelonida N

    Miki Tebeka Guest

    > 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.
    Miki Tebeka, Aug 6, 2012
    #2
    1. Advertising

  3. Gelonida N

    Miki Tebeka Guest

    > 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.
    Miki Tebeka, Aug 6, 2012
    #3
  4. Gelonida N

    Gelonida N Guest

    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.
    Gelonida N, Aug 6, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. plb
    Replies:
    2
    Views:
    335
  2. Michael Poeltl
    Replies:
    0
    Views:
    130
    Michael Poeltl
    Aug 7, 2012
  3. Gelonida N
    Replies:
    0
    Views:
    135
    Gelonida N
    Aug 7, 2012
  4. Peter Otten
    Replies:
    0
    Views:
    137
    Peter Otten
    Aug 7, 2012
  5. Chris Angelico
    Replies:
    0
    Views:
    125
    Chris Angelico
    Aug 7, 2012
Loading...

Share This Page