I am starting to compile my Python 3 project with Cython, and I would like to know if it's possible to reduce my current compile time workflow to a single instruction.
This is my setup.py as of now:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
extensions = [
Extension("v", ["version.py"]),
Extension("*", ["lib/*.py"])
]
setup(
name = "MyFirst App",
ext_modules = cythonize(extensions),
)
And this is what I run from shell to obtain my executables:
python3 setup.py build_ext --inplace
cython3 --embed -o main.c main.py
gcc -Os -I /usr/include/python3.5m -o main main.c -lpython3.5m -lpthread -lm -lutil -ldl
This whole process works just fine, I'd like to know if there is a way to also embed the last two instruction in the setup.py script.
Thank you
Yes, it is possible to reduce your current compile time workflow to a single instruction by using the subprocess module in the Python Standard Library.
Here is an example of how you can modify your setup.py script to compile your project with Cython and generate the executable in a single command:
import subprocess from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize extensions = [ Extension("v", ["version.py"]), Extension("*", ["lib/*.py"]) ] setup( name = "MyFirst App", ext_modules = cythonize(extensions), cmdclass = {'build_ext': build_ext_subprocess} ) class build_ext_subprocess(build_ext): def run(self): # Run the build_ext command build_ext.run(self) # Compile the main.py file using Cython subprocess.run(["cython3", "--embed", "-o", "main.c", "main.py"]) # Compile the main.c file and generate the executable subprocess.run(["gcc", "-Os", "-I", "/usr/include/python3.5m", "-o", "main", "main.c", "-lpython3.5m", "-lpthread", "-lm", "-lutil", "-ldl"])