Imported module goes out of scope (unbound local error)
<p>I am getting a strange "unbound local" error in a python package that seems to defy all logic. I can't get a MWE to reproduce it, but will try to explain succinctly and hope that someone might be able to offer some explanation of what is going on.</p>

<p>For the sake of this example <code>module</code> is a package I developed, and <code>Model</code> is a class within <code>module</code>. The definition of the <code>Model</code> class (model.py) looks like:</p>

<pre><code>import module

class Model:
    def __init__(self):
        print module

    def run(self):
        print module
</code></pre>

<p>Now, when I instantiate a <code>Model</code> in a script like this:</p>

<pre><code>from model import Model
m = Model()
m.run()
</code></pre>

<p><code>module</code> prints successfully within the <code>__init__</code>, but I get an unbound local error within the <code>run</code> function.</p>

<p>I attempted to diagnose this with pdb, which is where things got really weird, because if I add a pdb trace immediately prior to the <code>print module</code> line in the <code>run()</code> function, then I can successfully run <code>print module</code> without an unbound local error, but if I step to the next line then it throws the error. How can <code>module</code> be in the scope of <code>__init__()</code>, and in the scope of pdb, but not in the scope of <code>run()</code>?</p>

<p>I know this is not ideal since there is no MWE, but I cannot seem to reproduce this outside the context of the full code. I am hoping that someone will have an idea of what might possibly be going on and suggest some strategies to debug further.</p>