title: How to check if python module exists and can be imported
body: <p>I am using <a href="https://github.com/robhudson/django-debug-toolbar" rel="noreferrer">debug toolbar</a> with django and would like to add it to project if two conditions are true:</p>
  
  <ul>
  <li><code>settings.DEBUG</code> is <code>True</code></li>
  <li>module itself exists</li>
  </ul>
  
  <p>It's not hard to do the first one </p>
  
  <pre><code># adding django debug toolbar
  if DEBUG:
      MIDDLEWARE_CLASSES += 'debug_toolbar.middleware.DebugToolbarMiddleware',
      INSTALLED_APPS += 'debug_toolbar',
  </code></pre>
  
  <p>But how do I check if module exists?</p>
  
  <p>I have found this solution:</p>
  
  <pre><code>try:
      import debug_toolbar
  except ImportError:
      pass
  </code></pre>
  
  <p>But since import happens somewhere else in django, I need if/else logic to check if module exists, so I can check it in settings.py</p>
  
  <pre><code>def module_exists(module_name):
      # ??????
  
  # adding django debug toolbar
  if DEBUG and module_exists('debug_toolbar'):
      MIDDLEWARE_CLASSES += 'debug_toolbar.middleware.DebugToolbarMiddleware',
      INSTALLED_APPS += 'debug_toolbar',
  </code></pre>
  
  <p>Is there a way to do it?</p>