[python] A note when trying to use numpy with Cython

Introduction

What I want to do this time is ctypedef numpy.float64_t DOUBLE_t I want to define it as. To do this You need to do cimport numpy. I want to do these two ctypedef and cimport.

The environment looks like this. OS X, Yosemite Cython 0.23.4 Python 3.4.3

I get the following error when compiling cython.

Refer to the Cython 0.23.2 documentation: Building Cython code page and create the following two scripts.

test.pyx


import numpy
cimport numpy
ctypedef numpy.float64_t DOUBLE_t

setup.py


from distutils.core import setup
from Cython.Build import cythonize

setup(
    name = 'test',
    ext_modules = cythonize('test.pyx')
    )

When I use this to run python setup.py build_ext --inplace, I get the following error:

error.


sample.c:250: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 got an error saying that the file is missing. It seems that the path is different, so if you look at the site a little earlier, Something like this Cython 0.23.2 documentation: Compilation

Often, Python packages that offer a C-level API provide a way to find the necessary include files, e.g. for NumPy:

include_path = [numpy.get_include()]

It seemed to be common. For the time being, let's put this in setup.py.

setup.py


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

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

When I try to use this, I get an error again

error.


Unknown distribution option: 'include_path'

I was told that there is no such option. Looking at stackoverflow and so on, it seems that everyone does not use ʻinclude_path, but uses ʻinclude_dirs. For the time being, try s / include_path / include_dirs.

setup.py


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

setup(
    name = 'test',
    ext_modules = cythonize('test.pyx')
    include_dirs = [numpy.get_include()]
    )

When I tried it, it passed (o ・ ω ・ o) I don't know what it is, but it looks like it was done.

Summary

ʻInclude_dirs = [numpy.get_include ()] It sounds good to use. There are few new ones written in Japanese, so if you don't understand English, it can be painful. I compiled it this time, but there were a lot of warnings for ʻunused function, so maybe I have to fix it? (Because it is an unused function, it may be left alone)

Recommended Posts

[python] A note when trying to use numpy with Cython
Trying to handle SQLite3 with Python [Note]
Python Note: When assigning a value to a string
ImportError when trying to use gcloud package with AWS Lambda Python version
[Python / Pandas] A bug occurs when trying to replace a DataFrame with `None` with` replace`
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
I was addicted to trying Cython with PyCharm, so make a note
Things to note when initializing a list in Python
A note I was addicted to when running Python with Visual Studio Code
Solution when you want to use cv_bridge with python3 (virtualenv)
Python: How to use async with
[Python] I want to use only index when looping a list with a for statement
Use a macro that runs when saving python with vscode
I got stuck when trying to specify a relative path with relative_to () in python
How to use FTP with Python
A memo of misunderstanding when trying to load the entire self-made module with Python3
[Note] A story about trying to override a class method with two underscores in Python 3 series.
A program that failed when trying to create a linebot with reference to "Dialogue system made with python"
A note I was addicted to when creating a table with SQLAlchemy
[Introduction to Python] Let's use foreach with Python
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi
[Python] Explains how to use the range function with a concrete example
Gist repository to use when you want to try a little with ansible
A memorandum because I stumbled on trying to use MeCab in Python
Error when installing a module with Python pip
How to read a CSV file with Python 2/3
Send a message to LINE with Python (LINE Notify)
[Python] Road to a snake charmer (4) Tweak Numpy
I want to use MATLAB feval with python
Try to draw a life curve with python
I want to make a game with Python
[Note] Create a one-line timezone class with python
Wrap C with Cython for use from Python
Try to make a "cryptanalysis" cipher with Python
Decide to assign a laboratory with Python (fiction)
The easiest way to use OpenCV with python
Story of trying to use tensorboard with pytorch
I want to use Temporary Directory with Python2
A memo when creating a python environment with miniconda
Steps to create a Twitter bot with python
How to use tkinter with python in pyenv
Try to make a dihedral group with Python
Wrap C ++ with Cython for use from Python
Error when trying to install psycopg2 in Python
I want to write to a file with Python
A layman wants to get started with Python
Materials to read when getting started with Python
Python script to get note information with REAPER
It is more convenient to use csv-table when writing a table with python-sphinx
When I tried to create a virtual environment with Python, it didn't work
If you want to make a discord bot with python, let's use a framework
A story that I was addicted to when I made SFTP communication with python
[Python] Use JSON with Python
Use mecab with Python3
Use DynamoDB with Python
Use Python 3.8 with Anaconda
Use python with docker
Note to daemonize python
How to use numpy
How to convert / restore a string with [] in python
A memo connected to HiveServer2 of EMR with python