0

I am trying to invoke a Cython file in a Python script. I have read this answer and followed the instruction. However, even though I have successfully compiled the C code, PyCharm does not recognize the Cython file 'hello.pyx' while executing the import command, as shown in the screenshot below. What is the remedy?

enter image description here

The Cython file hello.c is generated by setup.py the content of which is shown below.

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

setup(name='Hello world app',
      ext_modules=cythonize("hello.pyx")
      )
Hans
  • 1,269
  • 3
  • 19
  • 38

1 Answers1

1

When you compile your module with cythonize, the resulting module will be put into a subfolder. By default, this subfolder is not in the PATHS list. To fix this problem you can just move the resulting file .dll file into the folder where your hello.py and hello.pyx are located.

An alternative approach is to add Extension like so:

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

from distutils.extension import Extension
exts = [Extension(name='hello',sources=['hello.pyx'])]

setup(name='Hello world app',
      ext_modules=cythonize(exts)
      )

And then compile with:

python setup.py build_ext --inplace

This code will put resulting .dll into the same folder as your source files.