title: dynamic module creation
body: <p>I'd like to dynamically create a module from a dictionary, and I'm wondering if adding an element to <code>sys.modules</code> is really the best way to do this.  EG</p>
  <pre><code>context = { a: 1, b: 2 }
  import types
  test_context_module = types.ModuleType('TestContext', 'Module created to provide a context for tests')
  test_context_module.__dict__.update(context)
  import sys
  sys.modules['TestContext'] = test_context_module
  </code></pre>
  <p>My immediate goal in this regard is to be able to provide a context for timing test execution:</p>
  <pre><code>import timeit
  timeit.Timer('a + b', 'from TestContext import *')
  </code></pre>
  <p>It seems that there are other ways to do this, since the Timer constructor takes objects as well as strings.  I'm still interested in learning how to do this though, since a) it has other potential applications; and b) I'm not sure exactly how to use objects with the Timer constructor; doing so may prove to be less appropriate than this approach in some circumstances.</p>
  <h3>EDITS/REVELATIONS/PHOOEYS/EUREKA:</h3>
  <ol>
  <li><p>I've realized that the example code relating to running timing tests won't actually work, because <code>import *</code> only works at the <em>module</em> level, and the context in which that statement is executed is that of a <em>function</em> in the <code>testit</code> module.  In other words, the globals dictionary used when executing that code is that of <code>__main__</code>, since that's where I was when I wrote the code in the interactive shell.  So that rationale for figuring this out is a bit botched, but it's still a valid question.</p>
  </li>
  <li><p>I've discovered that the code run in the first set of examples has the undesirable effect that the namespace in which the newly created module's code executes is that of the module in which it was <em>declared</em>, <em><strong>not</strong></em> <em>its own module</em>.  This is like way weird, and could lead to all sorts of unexpected rattlesnakeic sketchiness.  So I'm pretty sure that this is <strong>not</strong> how this sort of thing is meant to be done, if it is in fact something that the Guido doth shine upon.</p>
  </li>
  <li><p>The similar-but-subtly-different case of dynamically loading a module from a file that is not in python's include path is quite easily accomplished using <code>imp.load_source('NewModuleName', 'path/to/module/module_to_load.py')</code>.  This does load the module into <code>sys.modules</code>.  However this doesn't really answer my question, because really, what if you're running python on an <a href="http://thedailywtf.com/Articles/The-cbitmap.aspx" rel="nofollow noreferrer">embedded platform with no filesystem</a>?</p>
  </li>
  </ol>
  <p>I'm battling a considerable case of information overload at the moment, so I could be mistaken, but there doesn't seem to be anything in the <code>imp</code> module that's capable of this.</p>
  <p>But the question, essentially, at this point is how to set the global (ie module) context for an object.  Maybe I should ask that more specifically?  And at a larger scope, how to get Python to do this while shoehorning objects into a given module?</p>