This is going to be a bit long, sorry. You might want to skip to the end first, where I give the actual error message. The rest of the post is background, but probably necessary background.
I've got a new-ish Mac running MacOS 10.12.5. It came with python, numpy, scipy (albeit relatively old: python 2.7.10 and scipy 0.13); and I added cython. Since then I've been using cython painlessly like always.
The problem came yesterday when I needed to use a package in scipy that wasn't added until scipy version 0.14. And it turns out that, on this version of MacOS, it won't let me upgrade scipy, even as sudo. But there is a workaround which I found suggested here:
http://docs.python-guide.org/en/latest/starting/install3/osx/ - install python3, which will give me separate python2 and python3 commands. Then I can leave the command "python" for system programs, and do all my personal work using python2 (or python3 if I want). Then when I upgraded scipy, both python2 and python3 pick up scipy 0.19 (but the original "python" does not). The download also aliases pip2 and pip3 to be the python "pip" command for updating python2 and python3 respectively.
These new python downloads didn't have cython. So I downloaded it again. Here is an edited transcript (**** is where I have edited out my username):
$ python2
Python 2.7.14 (default, Sep 25 2017, 09:53:22)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cython
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cython
$ pip2 install Cython
Collecting Cython
Downloading Cython-0.27.2-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.6MB)
100% |████████████████████████████████| 4.6MB 234kB/s
Installing collected packages: Cython
Exception:
Traceback (most recent call last):
[most of traceback redacted]
File "/usr/local/lib/python2.7/site-packages/pip/_vendor/distlib/util.py", line 407, in write_binary_file
with open(path, 'wb') as f:
IOError: [Errno 13] Permission denied: '/usr/local/bin/cython'
$ sudo pip2 install Cython
Password:
The directory '/Users/****/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/****/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: Cython in /usr/local/lib/python2.7/site-packages
$ python2
Python 2.7.14 (default, Sep 25 2017, 09:53:22)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cython
import cython
So cython appears to have been installed, and I can run cython so long as I'm not using numpy. But when I try to call cimport numpy, I get problems. I use the same format of compile file as I've been using for years:
from distutils.core import setup
from Cython.Build import cythonize
import numpy as np
setup( include_dirs = [np.get_include()],
ext_modules = cythonize("cyrun_diffusion.pyx")
)
I compiled with the command:
$ python2 cyrun_diffusion_compile.py build_ext --inplace > cyrun_diffusion_compile.log 2>&1
and I get this error message (i.e. the contents of cyrun_diffusion_compile.log):
running build_ext
building 'cyrun_diffusion' extension
clang -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/usr/local/opt/openssl/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c cyrun_diffusion.c -o build/temp.macosx-10.12-x86_64-2.7/cyrun_diffusion.o
cyrun_diffusion.c:538:10: fatal error: 'numpy/arrayobject.h' file not found
#include "numpy/arrayobject.h"
^
1 error generated.
error: command 'clang' failed with exit status 1
I assume this is in reaction to the line "cimport numpy as np" in my source file.
So finally here's my question: why can't Cython find numpy? And what should I try to do to fix it?