title: How do I load a Python module with custom globals using importlib?
body: <p>I'm trying to put together a small build system in Python that generates Ninja files for my C++ project. Its behavior should be similar to CMake; that is, a <code>bldfile.py</code> script defines rules and targets and optionally recurses into one or more directories by calling <code>bld.subdir()</code>. Each <code>bldfile.py</code> script has a corresponding <code>bld.File</code> object. When the <code>bldfile.py</code> script is executing, the <code>bld</code> global should be predefined as that file's <code>bld.File</code> instance, <em>but only in that module's scope</em>.</p>
  
  <p>Additionally, I would like to take advantage of Python's bytecode caching somehow, but the <code>.pyc</code> file should be stored in the build output directory instead of in a <code>__pycache__</code> directory alongside the <code>bldfile.py</code> script.</p>
  
  <p>I know I should use <code>importlib</code> (requiring Python 3.4+ is fine), but I'm not sure how to:</p>
  
  <ol>
  <li>Load and execute a module file with custom globals.</li>
  <li>Re-use the bytecode caching infrastructure.</li>
  </ol>
  
  <p>Any help would be greatly appreciated!</p>