Import Cython's pyx file in Python

2021-12-20
#Unix #Python

1. Introduction

The Cython’s file namexxx.pyx needs to import into Python file.

2. Steps

  1. Generate the namexxx.so file:
python setup.py build_ext --inplace

The general content of setup.py is:

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

setup(
  include_dirs=[numpy.get_include()],
  ext_modules = cythonize(["*.pyx"]),
)

This will generate the build folder and the namexxx.so and namexxx.c files.

  1. Add these commands into namexxx.pyx file
import pyximport
pyximport.install()
  1. import the right name
import namexxx

3. Reference