title: How to test if a Python object is a module?
body: <p>Python modules are objects too. So I would imagine it could be possible to test if a given object is a module(/package) this way:</p>
  
  <pre><code>&gt;&gt;&gt; import sys, os, my_module
  &gt;&gt;&gt; isinstance(sys, ModuleClass)
  True
  &gt;&gt;&gt; isinstance(os, ModuleClass)
  True
  &gt;&gt;&gt; isinstance(my_module, ModuleClass)
  True
  &gt;&gt;&gt; isinstance(5, ModuleClass)
  False
  </code></pre>
  
  <p>Except "ModuleClass" is just a name I invented and I can't find in the docs if anything like it actually exists and what it's called.</p>
  
  <p>Is there such a class at all? Is it somewhere in the docs that I've missed?</p>
  
  <p>If not, is there another way to see if an object is a module? Even better if it's  implementation-agnostic.</p>
  
  <p>(I'm using Python 2.7, but I guess it would also be interesting to know if 3.x has some new solution.)</p>
  
  <p>It sounds like <code>hasattr(obj, '__package__')</code> (<a href="http://www.python.org/dev/peps/pep-0366/" rel="nofollow">PEP 366</a>) is a possible stopgap, but AFAICT there's no special reason to feel sure that some future update won't put <code>__package__</code> into some other, totally unrelated objects. I'd prefer a solution that feels a bit more robust.</p>