Cython
pip install -r requirements.txt
python setup.py build_ext --inplace
pip install .
A .pyx or .py file is compiled by Cython to a .c file
The .c file is compiled by a C compiler to a .so file which can be imported directly into a Python session
hello.pyx:
def say_hello_to(name -> str = "Ray") -> str:
print(f"Hello {name}!")
pyproject.toml:
[build-system]
requires = ["setuptools", "setuptools-scm", "Cython", "numpy"]
build-backend = "setuptools.build_meta"
[project]
name = "PACKAGE"
description = "Find contors"
authors = [{name = "NAME", email = "EMAIL"}]
version = "2022.11.1"
dependencies = ["Cython", "numpy"]
[tool.setuptools]
py-modules = ["_custom_build"]
[tool.setuptools.cmdclass]
build_py = "_custom_build.build_py"
_custom_build.py:
# https://stackoverflow.com/a/74196255/6046019
import numpy
from setuptools import Extension
from setuptools.command.build_py import build_py as _build_py
class build_py(_build_py):
def run(self):
self.run_command("build_ext")
return super().run()
def initialize_options(self):
super().initialize_options()
if self.distribution.ext_modules is None:
self.distribution.ext_modules = []
self.distribution.ext_modules.append(
Extension(
"findiso",
sources=["findiso/findiso.pyx", "findiso/findContours.cpp"],
include_dirs=[numpy.get_include(), "."],
language="c++",
libraries=["stdc++"],
extra_compile_args=["-std=c++11"],
extra_link_args=["-std=c++11"],
)
)