imp.load_source loads wrong module
<p>I have two supposedly identical systems. On both systems, I run the same software, but on one of the two it doesn't function correctly.</p>

<p>I'm trying to run function in a user-supplied <code>.py</code> file. I've reduced this to the following basic code that reproduces the error:</p>

<pre><code>import imp
with open("test_scripts/load_offsets.py") as fp:
    module = imp.load_source("load_offsets", "test_scripts", fp)

dir(module)
</code></pre>

<p>On the first system, the output is correct:</p>

<pre><code>['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'os', 'test_load_offsets']
</code></pre>

<p>I see one function called <code>test_load_offsets</code>, as expected.</p>

<p>On the second system, I get the following output:</p>

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

<p>Note that I see a different function now: <code>test_reset_position</code>. However, on both systems, the file <code>test_scripts/load_offsets.py</code> is identical! More importantly, there is no function <code>test_reset_position</code> in this file. </p>

<p>There is, however, a function <code>test_reset_position</code> in a different file, namely <code>test_scripts/reset_position.py</code>. In addition, in the directory where I executed the code sample, there is a file <code>test_scriptsc</code> (this is the case on both systems). It contains, I think, byte code, but on the system where I see the problem I can recognise parts of the file <code>test_scripts/test_reset_position.py</code> in it. If I remove <code>test_scriptsc</code>, it works fine again, until load a different file and then the problem starts again.</p>

<p>So, my conclusion is that on the system with the problem, the file <code>test_scriptsc</code> is not updated correctly. However, I do not understand why, nor do I understand why the problem is only on one of the two systems. The only difference I can find between the two systems is that the problematic system runs Python Python 2.7.12 on Ubuntu Linux 16.04.1 while the system without the problem runs Python 2.7.11+ on Ubuntu 16.04.</p>

<p>Can someone help me find out what is going on here? I have no clue what to look for...</p>

<p>The reason why it is failing is because I'm doing it wrong. The second argument to <code>load_source</code> should be the full path to the source file, not just to the directory containing it <a href="https://docs.python.org/2/library/imp.html#imp.load_source" rel="nofollow" title="Python 2.7.12 documentation">Python 2.7.12 documentation</a>.
I'm not sure why I came up with my implementation, nor do I know why Python 2.7.11 was accepting it anyway, but the solution is to do it the right way:</p>

<pre><code>with open("test_scripts/load_offsets.py") as fp:
    m = imp.load_module("load_offsets", fp, "test_scripts/load_offsets.py", ("py","r",imp.PY_SOURCE))

dir(m)
</code></pre>

<p>This correctly gives:</p>

<pre><code>['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'os', 'test_load_offsets']
</code></pre>

<p>I think the file <code>test_scriptsc</code> was an attempt of byte compiling a <code>.py</code> file, but since I didn't give the full path, I ended up with something that was a sort-of-compiled directory. Apparently Python 2.7.11 handled this differently than Python 2.7.12.</p>

python python-2.7 python-import