(Added on 2020.04.05) For Windows, the road to compiling is quite confusing. There are various hints on the Ichiou Cython official page, but ... it's tedious to read:
-Article to get a Python compilation environment: Recent versions seem to need something called the Windows SDK C / C ++ Compiler .. Basically you need a compiler for the version of the Windows SDK that Python was built on (see this Python wiki page (https://wiki.python.org/moin/WindowsCompilers) for more information). -Tips for installing with Anaconda etc.: There isn't much new information here, but just in case
Furthermore, in order to use the compiler from Cython, it is necessary to run Python (Cython) on a terminal emulator with the build environment set up correctly. The simplest is
- Make the python environment work with the standard Windows shell
- Start a command prompt of Visual Studio and run python setup.py there
But if you want to use Anaconda's virtual environment, it can be very confusing. During trial and error, where and what kind of environment variables should be set up so that they can be used on the anaconda prompt ...
Use pip and you're done right away. I think I didn't need administrator privileges ...
$ pip install cython
Copy the code from Cython: Basic tutorial as it is and check if it can be compiled:
python setup.py build_ext --inplace
. For Windows, you need to do it on the developer command prompt to use the required commands and include files.When you run setup.py
, you may not add the --inplace
option, or it may be installed in the Python library (unconfirmed).
If the compilation is successful, setup.py will end with a "Finished" message. In that case, you can import the compiled file by doing `ʻimport
On the other hand, if the compilation fails, a "Failed" message will appear. Go back a little before the output of the terminal emulator to see more details about the error. Most of the time, I think it's a mistake in the .pyx file or the include / link file is missing.
Excerpt from Stack Overflow Answer.
When using numpy from Cython, C header files of numpy library are required. It's easy to understand when compiling directly using gcc or the like, but the -I
option cannot be used when compiling via setup.py.
In this case, use the `ʻExtensionobject of
distutils``:
setup.py
from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy
setup(
ext_modules=[
Extension("my_module", ["my_module.c"],
include_dirs=[numpy.get_include()]),
],
)
The second argument in this case specifies a list of source files (absolute or relative path). List the include paths you want to add with the `ʻinclude_dirs`` keyword argument.
Or if you look at the Cython documentation (http://cython.readthedocs.io/en/latest/src/reference/compilation.html), the cythonize ()
itself has an option called `ʻinclude_dirs`` Apparently:
setup.py
from distutils.core import setup
from Cython.Build import cythonize
setup(
name = "My hello app",
ext_modules = cythonize("src/*.pyx", include_path = [...]),
)
numpy
provides a function called get_include ()
, so you can use it, and even native libraries can use pkg-config or whatever output.
Recommended Posts