title: Dynamic Loading of Python Modules
body: <p>I'm trying to dynamically load modules I've created.</p>
  
  <p>Right now this works properly:</p>
  
  <pre><code>import structures.index
  </code></pre>
  
  <p>But if I try the same thing by importing it dynamically, it fails.</p>
  
  <pre><code>struct = __import__("structures.index")
  </code></pre>
  
  <p>Error supplied is:</p>
  
  <pre><code>Error ('No module named structures.index',)
  </code></pre>
  
  <p>Any ideas why?</p>
  
  <hr>
  
  <p>Edit: When using full scope (it sort of works?):</p>
  
  <pre><code>struct = __import__("neoform.structures.index")
  </code></pre>
  
  <p>This doesn't throw any errors, however, it isn't loading the index module, it's loading the "neoform" module instead.</p>
  
  <p>The result of "struct" is:</p>
  
  <pre><code>&lt;module 'neoform' from '/neoform/__init__.py'&gt;
  </code></pre>
  
  <p>Also, as a side question, how can I then instantiate a class within a dynamically loaded module? (assuming all the modules contain a common class name).</p>
  
  <p>Edit: Solution: (thanks coonj &amp; Rick) This ended up being what worked. Not sure why (yet), but the <code>fromlist</code> had to be something "anything apparently, since it worked when I put the letter "a" as a value (strange, given that the file only had 1 class in it).</p>
  
  <pre><code>def get_struct_module(self, name):
      try:
          return = __import__("neoform.structures." + name, fromlist='*')
      except ImportError, e:
          self.out.add("Could not load struct: neoform.structure." + name + "\n\n" + "Error " + str(e.args))
  </code></pre>