title: Dynamically import packages with multiple files in Python
body: <p>I am trying to dynamically import modules in python.
  I need something like a plugin import system.</p>
  
  <p>I use the following code for import of a module, and it works fine, as long as the entire code of the module is in the same file.</p>
  
  <p><code>caller.py</code> code:</p>
  
  <pre><code>PluginFolder = "./plugins"
  MainModule = "__init__"
  possibleplugins = os.listdir(PluginFolder)
  for possible_plugin in possibleplugins:
      location = os.path.abspath(os.path.join(PluginFolder, possible_plugin))
      if not os.path.isdir(location) or not MainModule + ".py" in os.listdir(location):
          continue
      info = imp.find_module(MainModule, [location])
      plugin = {"name": possible_plugin, "info": info}
  </code></pre>
  
  <p>After that, I use the load_module method to load the module:</p>
  
  <pre><code>module = imp.load_module(MainModule, *plugin["info"])
  </code></pre>
  
  <p>The structure of the directories is as follows:</p>
  
  <pre><code>Project/
    --plugins/
    ----plugin_1/
    -------__init__.py
    -------X.py
    caller.py
  </code></pre>
  
  <p>Everything works great when all the methods are in the same file (<code>__init__.py</code>).</p>
  
  <p>When the method I use in <code>__init__.py</code> calls another method from another file (in the same package) than an error saying "No module named X"</p>
  
  <p>the <code>__init__.py</code> code fails at the line:</p>
  
  <pre><code>import X
  </code></pre>
  
  <p>I also tried </p>
  
  <pre><code>import plugin_1.X
  </code></pre>
  
  <p>and other variations.</p>
  
  <p>Just to make sure you guys understand- importing and using the module in a normal way (not dynamically) works fine.</p>
  
  <p>What am I missing?
  Is there another way to do this? Maybe use the <code>__import__</code> method, or something else?</p>