[Django]-How to check if python module exists and can be imported

47👍

✅

You can use the same logic inside your function:

def module_exists(module_name):
    try:
        __import__(module_name)
    except ImportError:
        return False
    else:
        return True

There is no performance penalty to this solution because modules are imported only once.

Leave a comment