Creating Anonymous Modules in Python
<p><strong>Edit: I've added some more info on why I'm looking to do this.</strong></p>

<p>The example code below is purely for example purposes. In the end, I need to have my code walk over a directory structure, and when it finds a python file, import the file to grab data and possibly function definitions from it. That will then be used when determining what to do with the remaining files in the directory (which are HTML templates, etc.)</p>

<p>Because the idea is to make the directory structure be more suited for holding HTML templates and images, and the python files to really are more of a data container (although every once and awhile there will be logic in terms of function), I don't want to have <code>__init__.py</code> files all over the place to make sure that the python modules are part of a package, and I feel that messing with the sys.path is the wrong solution (there might be  "meta.py" files in multiple directories).</p>

<p>So, I feel loading a python file by it's source to be the best solution. The problem with this is what I've shown below.</p>

<p>Imagine that I have code that does the following:</p>

<p>main.py:</p>

<pre><code>import imp

a = imp.load_source('blah', 'a.py')
print dir(a)
b = imp.load_source('blah', 'b.py')
print dir(b)
</code></pre>

<p>a.py:</p>

<pre><code>a = 'a'
</code></pre>

<p>b.py:</p>

<pre><code>b = 'b'
</code></pre>

<p>The results of running main.py are:</p>

<pre><code>['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'a']
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'a', 'b']
</code></pre>

<p>What I was hoping to happen was that the second call to load_source creates a <em>new</em> module and returns it, but it looks like it actually will overwrite the existing one.</p>

<p>For now, my solution is to change the 'blah' string in the load_source function to be some name that none of these modules will share (such as their filename), but I was wondering if there was some better way.</p>
