title: How to use the __import__ function to import a name from a submodule?
body: <p>I'm trying to replicate <code>from foo.bar import object</code> using the <code>__import__</code> function and I seem to have hit a wall.</p>
  
  <p>A simpler case <code>from glob import glob</code> is easy: <code>glob = __import__("glob").glob</code></p>
  
  <p>The problem I'm having is that I am importing a name from a subpackage (i.e. <code>from foo.bar</code>):</p>
  
  <p>So what I'd like is something like</p>
  
  <pre><code>string_to_import = "bar"
  object = __import__("foo." + string_to_import).object
  </code></pre>
  
  <p>But this just imported the top-level <code>foo</code> package, not the <code>foo.bar</code> subpackage:</p>
  
  <pre><code>__import__("foo.bar")
  &lt;module 'foo' from 'foo/__init__.pyc'&gt;
  </code></pre>