Python: free variable 'numpy' referenced before assignment in enclosing scope
<p>I am adding some code into an existing class for testing purposes.  Normally this class, <code>eigensystem_CUDA_implementation</code>, relies on some functions and attributes of its parent.  When this class is imported independently of the rest of the program, I'd like to substitute the usual parent class for a testParent class.</p>

<p>If this is a bad way to test code and I should do this completely differently, I'm open to that suggestion.</p>

<p>When initializing an <code>eigensystem_CUDA_implementation</code> object, the error is:</p>

<blockquote>
  <p>NameError: free variable 'np' referenced before assignment in enclosing scope</p>
</blockquote>

<p>which traces back to the line <code>self.mat = np.matrix(...</code></p>

<p>There is much more that relies on NumPy and pyCUDA in <code>eigensystem_CUDA_implementation</code> that is not shown.  Can a class import a module that it relies on?  Haven't tested this yet because of the first error.</p>

<pre><code>class eigensystem_CUDA_implementation:
    def __init__(self, parent = None, max_time = 60, delta = 10**(-32)):
            # For testing purposes, when class is utilized independently
            if(not parent):
                    # testing mode
                    if(not sys.modules.has_key("numpy")):
                            import numpy as np
                            import pycuda.driver as cuda
                            import pycuda.autoinit
                            from pycuda.compiler import SourceModule
                    class testParent:
                            def __init__(self, size = 10):
                                    self.size = size
                                    self.delta = 10**(-32)
                                    self.num_site_types = 8
                                    self.mat =  np.matrix(np.random.random((self.size,self.size)).astype(np.float64))
                            def get_mutation_selection_matrix(self, alpha):
                                    return self.mat
    ...
</code></pre>

<p>One potential problem: <code>not sys.modules.has_key("numpy")</code> would show NumPy as imported whether it is named "np" or something else.  The rest of the code uses "np" though so I'm ignoring this.</p>

<p>Thanks for any suggestions</p>
