title: Adding `cimport numpy` to pyx file compiles ok but throws ValueError when run
body: <p>I'm attempting to optimise a simple cython routine I've written that accepts a 1D numpy array as input and returns a 1D numpy array as output.</p>
  
  <p>I have a naive version working, and am now attempting to do some <a href="http://docs.cython.org/src/tutorial/numpy.html" rel="nofollow">standard optimisations</a> to the numpy parts of the cython code.</p>
  
  <p>However, as soon as I add <code>cimport numpy</code> to my cython source file, I am unable to run the extension module (though it compiles okay), throwing a <code>ValueError: numpy.dtype does not appear to be the correct type object</code>.</p>
  
  <p>My numpy version is 1.6.0 and python version 2.6.5 (default, Ubuntu 10.04 apt-get installations)</p>
  
  <p>Minimal example causing the problem:</p>
  
  <hr>
  
  <h2>test_ext.pyx</h2>
  
  <pre><code>import numpy as np
  cimport numpy as np #Commenting this line un-breaks the extension
  
  def test_ext(refls, str mode, int nguard, int nblock):
  
      cdef int i
      cdef int _len = refls.shape[0]
      _out = np.zeros([_len])
  
      for i in range(_len):
          _out[i] = refls[i] + 1
      return _out
  </code></pre>
  
  <hr>
  
  <h2>setup.py</h2>
  
  <pre><code>from distutils.core import setup
  from distutils.extension import Extension
  from Cython.Distutils import build_ext
  
  ext_modules = [Extension("test_ext", ["test_ext.pyx"])]
  
  setup(
      name = 'test',
      cmdclass = {'build_ext': build_ext},
      ext_modules = ext_modules
  )
  </code></pre>
  
  <hr>
  
  <h2>run_ext.py</h2>
  
  <pre><code>import numpy
  from test_ext import test_ext
  
  a = np.array([x for x in range(260)])
  _res = test_ext.test_ext(a, 'avg', 2, 3)
  </code></pre>
  
  <h2>build with:</h2>
  
  <p><code>python setup.py build_ext --inplace</code></p>
  
  <h2>My build output:</h2>
  
  <pre><code>running build_ext
  cythoning my_ext.pyx to my_ext.c
  building 'my_ext' extension
  gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.6 -c my_ext.c -o build/temp.linux-x86_64-2.6/my_ext.o
  /usr/include/python2.6/numpy/__multiarray_api.h:968: warning: ‘_import_array’ defined but not used
  my_ext.c:732: warning: ‘__pyx_k_3’ defined but not used
  my_ext.c:733: warning: ‘__pyx_k_4’ defined but not used
  my_ext.c:753: warning: ‘__pyx_k_24’ defined but not used
  my_ext.c:759: warning: ‘__pyx_k_26’ defined but not used
  my_ext.c:760: warning: ‘__pyx_k_27’ defined but not used
  my_ext.c:1118: warning: ‘__pyx_pf_5numpy_7ndarray___getbuffer__’ defined but not used
  my_ext.c:1876: warning: ‘__pyx_pf_5numpy_7ndarray___releasebuffer__’ defined but not used
  gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions build/temp.linux-x86_64-2.6/my_ext.o -o /home/path/to/my_ext.so
  </code></pre>
  
  <p>and run then <code>run_ext.py</code>.</p>
  
  <p>With the <code>cimport</code> line uncommented, I get the error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "run_ext.py", line 2, in &lt;module&gt;
      from test_ext import test_ext
    File "numpy.pxd", line 43, in test_ext (test_ext.c:2788)
  ValueError: numpy.dtype does not appear to be the correct type object
  </code></pre>
  
  <hr>
  
  <p>What causes this and how can I fix it to continue optimising my extension? I have attempted to continue with the optimisations as per the link above, but however far along I get, nothing un-breaks this issue for me.</p>
  
  <p>I've seen some other people with this problem that had version problems with python/numpy, but I don't think that's the case for me (though I'm open to suggestion there). Any suggestions appreciated.</p>