0

I want to use Numpy in Cython, but encountered the following error. This error happens even if I run the simple code, so it should be an issue related to importing Numpy.

My environment:

  • OS X Yosemite
  • Python 3.4.3

setup.py:

import numpy as np
from distutils.core import setup
from Cython.Build import cythonize

setup(
        name = 'my_code',
        ext_modules = cythonize('my_code.pyx'),
        include_path = [numpy.get_include()]
)

my_code.pyx:

cimport numpy as np

cdef int a 

Execute in Terminal:

$ python3 setup.py build_ext --inplace
Traceback (most recent call last):
  File "/Users/***/setup.py", line 1, in <module>
    import numpy as np
  File "/usr/local/lib/python3.4/site-packages/numpy/__init__.py", line 180, in <module>
    from . import add_newdocs
  File "/usr/local/lib/python3.4/site-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/local/lib/python3.4/site-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/usr/local/lib/python3.4/site-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/usr/local/lib/python3.4/site-packages/numpy/core/__init__.py", line 58, in <module>
    from numpy.testing import Tester
  File "/usr/local/lib/python3.4/site-packages/numpy/testing/__init__.py", line 10, in <module>
    from unittest import TestCase
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/__init__.py", line 59, in <module>
    from .case import (TestCase, FunctionTestCase, SkipTest, skip, skipIf,
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 253, in <module>
    class _CapturingHandler(logging.Handler):
AttributeError: 'module' object has no attribute 'Handler'
2
  • Does numpy by itself work? Commented Nov 24, 2016 at 17:36
  • @hpaulj Yes. If I launch Python on Terminal, I can import Numpy. (Or I can also use it on Jupyter Notebook) Commented Nov 25, 2016 at 1:13

1 Answer 1

2

Old question but I ran into this because I had a very similar issue with numpy.

It turns out I had a directory named logging as a sibling of the script I was trying to run. So the problem was a simple naming collision between my local project folder and the logging module numpy expected to find. Renaming the project logging folder solved the issue for me.

Looking at OP's error message, I suspect this was the case for them as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.