title: import a submodule with its name from a module
body: <p>I have a function called <code>prepared_db</code> in submodule <code>db.db_1</code>:</p>
  
  <pre><code>from spam import db
  
  submodule_name = "db_1"
  func_name = "prepare_db"
  func = ...
  </code></pre>
  
  <p>how can I get the function by the submodule name and function name in the context above?</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>To respond @histrio 's answer, I can verify his code works for <code>os</code> module. But it does not work in this case. To create the example:</p>
  
  <pre><code>$ mkdir -p spam/db
  $ cat &gt; spam/db/db_1.py
  def prepare_db():
      print('prepare_db func')
  $ touch spam/db/__init__.py
  $ PYTHONPATH=$PYTHONPATH:spam
  </code></pre>
  
  <p>now, you can do the import normally:</p>
  
  <pre><code>&gt;&gt;&gt; from spam.db.db_1 import prepare_db
  &gt;&gt;&gt; prepare_db()
  prepare_db func
  </code></pre>
  
  <p>but if you do this dynamically, I get this error:</p>
  
  <pre><code>&gt;&gt;&gt; getattr(getattr(db, submodule_name), func_name)
  ---------------------------------------------------------------------------
  AttributeError                            Traceback (most recent call last)
  &lt;ipython-input-9-1b6aa1216551&gt; in &lt;module&gt;()
  ----&gt; 1 getattr(getattr(db, submodule_name), func_name)
  
  AttributeError: module 'spam.db.db_1' has no attribute 'prepared_db'
  </code></pre>