Python importlib import_module Relative Import of Module
<p>According to <a href="https://stackoverflow.com/questions/10675054/how-to-import-a-module-in-python-with-importlib-import-module">this answer</a>, you can use <code>importlib</code> to <code>import_module</code> using a relative import like so:</p>

<pre><code>importlib.import_module('.c', 'a.b')
</code></pre>

<p>Why doesn't relative import work for sklearn.feature_extraction.text ?</p>

<pre><code>importlib.import_module('.text', 'sklearn.feature_extraction')
</code></pre>

<p>I verified that <code>text</code> is a module with:</p>

<pre><code>from types import ModuleType
import sklearn.feature_extraction.text
isinstance(sklearn.feature_extraction.text, ModuleType)
</code></pre>

<p>Returns</p>

<pre><code>True
</code></pre>

<p><strong>Edit</strong></p>

<p>By "doesn't work", I mean it doesn't import the module.</p>

<p>I am using Python 3.4</p>

<p>Absolute way works:</p>

<pre><code>import importlib
text = importlib.import_module('sklearn.feature_extraction.text')
tfidf = text.TfidfVectorizer()
</code></pre>

<p>Relative way doesn't:</p>

<pre><code>import importlib
text = importlib.import_module('.text', 'sklearn.feature_extraction')
Traceback (most recent call last):
  File "&lt;pyshell#28&gt;", line 1, in &lt;module&gt;
    text = importlib.import_module('.text', 'sklearn.feature_extraction')
  File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "&lt;frozen importlib._bootstrap&gt;", line 2249, in _gcd_import
  File "&lt;frozen importlib._bootstrap&gt;", line 2199, in _sanity_check
SystemError: Parent module 'sklearn.feature_extraction' not loaded, cannot perform relative import
</code></pre>