title: How to import a module in Python 3 from a string?
body: <p>A <a href="https://stackoverflow.com/questions/3799545/dynamically-importing-python-module">solution</a> for this problem is available for Python 2, but it uses the <code>imp</code> module which is deprecated in Python 3.</p>
  
  <p><code>imp</code> has been replaced by <a href="https://docs.python.org/3/library/importlib.html#importlib.import_module" rel="nofollow noreferrer"><code>importlib</code></a> which works well for file based imports. Specifically, <code>importlib.import_module</code> requires a file name - not a string or a file handler.</p>
  
  <p>I made a workaround by dumping the contents of the URL to a file and importing it</p>
  
  <pre><code>def initlog():
      modulename = '_mylogging'
      try:
          import _mylogging
      except ImportError:
          r = requests.get('http://(...)/mylogging.py')
          with open(modulename+'.py', "w") as f:
              f.write(r.text)
      finally:
          import _mylogging
  
      return _mylogging.MYLogging().getlogger()
  </code></pre>
  
  <p>but I would like to avoid the intermediate file.</p>
  
  <p>Putting the security, network performance and availability issues aside - <strong>is there a way to feed a string to <code>importlib</code>?</strong> (or from a file handler, in which case I would use <a href="https://docs.python.org/3/library/io.html#io.StringIO" rel="nofollow noreferrer"><code>io.StringIO</code></a>)</p>