Cannot import my Cython module in python. Why it does not work?

36 views Asked by At

I created and built a Cython file in a separated package from other python packages, the project has the following directory structure:

cython_code
    cython_file.pyx
    setup.py
program
    main.py

Installing of Cython was done by using PyCharm. Build was done using command:

python setup.py build_ext --inplace

and build was created with corresponding .pyd, .c files and build folder.

But for some reason I cannot import that module. code in order to import module

Also I tried place .pyx file in project root folder and compile it there, but I got same problem with simple import (It cannot find cython module).

Here is my setup.py:

from setuptools import setup
from Cython.Build import cythonize
import numpy

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

Please, help me deal with this problem...

1

There are 1 answers

3
mudskipper On

The .pyx file is a source file used to build the actual extension code file (which will have the extension .so or .dll); it is not used at runtime by Python. To make your package importable in Python you need to either (1) run pip install . and actually install the build artifacts or (2) locate the built .so file and make sure that is on your PYTHONPATH.

For an initial project you could follow up the steps in the Cython docs.