5
votes

I’m trying to follow this tutorial on using pyx, pxd, and cimport to create and use extension types.

When compiling the Cython file in terminal, I am getting an error that I don’t know how to correct for:

cdef class CythonClass: in the pyx file is indicated as the line of error.

File "/Library/Python/2.7/site-packages/Cython/Build/Dependencies.py", line 1056, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: CythonClass.pyx

I am using Cython version .25 (and have tried other versions as well, each installed with pip install cython) on MacOS Sierra. Python version is 2.7.10.

As suggested, I installed gcc (Command Line Tools for Xcode 8.2), but am still receiving the error.

File contents:

pxd:

cdef class CythonClass:
    cdef:
        list list1
        dict d1, d2, d3, d4, d5, d6

pyx:

cdef class CythonClass:
    def __init__(self):
        self.list1 = []
        self.d1 = {}
        self.d2 = {}
        self.d3 = {}
        self.d4 = {}
        self.d5 = {}
        self.d6 = {}

setup.py (invoked by python setup.py build_ext --inplace in terminal):

from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize(["CythonClass.pyx"]))

At least presently, I'll attempt to compile without using pxd files, since the compilation went through for the extension types. However, the main function that imports the extension types is not compiling (with error: extTypeName is not a type identifier).

1
Hi - I just tried this in my machine but I actually did not get any error in compilation. I using Windows 10, Python 3.5 64bit, with Cython version 0.24.1. I'm not sure what is the cause of your problem. Sorry! - joon
How did you install Cython? The log makes it look like you are using the system Python and install Cython system-wide but that does not tell how. The Cython doc docs.cython.org/en/latest/src/quickstart/install.html suggest to install Apple's XCode to obtain a version of gcc. - Pierre de Buyl
Hi, I also tested and found no error. The problem might be in annoying details like the filenames in the path or in the presence of invisible characters in the source code. Can you attach the files and provide a full log of the error? - Pierre de Buyl
More detail: a hyphen - in the modulename is not allowed in Python. - Pierre de Buyl
You need the pxd file only if you want to import the cython module into another cython module. But then you don't import CythonClass.pxd in CythonClass.pyx. - Pierre de Buyl

1 Answers

0
votes

I found a solution at: Cythonize by Command Line

Read the page but more specifically go to the section: "Compiling with the cythonize command".

You create the .pyd or .so file:

cython yourmod.pyx
cythonize -a -i yourmod.c

Worked for me.