0

I need to check if module exists (without checking if their import modules exists), import the module to check if it exists is a bad idea because:

foo/module_A.py:

import invalid_module_asdfghjkl


class ClassA:
    pass

foo/module_B.py:

try:
    __import__('foo.module_A')
except ImportError, e:
    >>> No module named invalid_module_asdfghjkl

So, My module exists, but not one of their imported modules, how can I check IF MY MODULE EXISTS when it has invalid imports?

Sam
  • 7,252
  • 16
  • 46
  • 65
fj123x
  • 6,904
  • 12
  • 46
  • 58
  • 2
    Have you looked at [`imp.find_module`](https://docs.python.org/2.7/library/imp.html#imp.find_module)? – BrenBarn Jun 19 '14 at 19:00
  • 1
    The name of your module will be in the error message if it's the one that can't be found, otherwise it's something else you're importing from there. – kindall Jun 19 '14 at 19:01
  • @BrenBarn, find_module crashes too because internally performs an import – fj123x Jun 19 '14 at 19:02

0 Answers0