Cpython编译

代码文件setup_c.py如下


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


def all_path(dirname):
    result = []  # 所有的文件
    count = 0
    for maindir, subdir, file_name_list in os.walk(dirname):
        if count != 0:
            for filename in file_name_list:
                if filename.endswith('.py') and filename != 'manage.py':
                    if '.py' in filename and filename != 'setup_c.py' and '.pyc' not in filename and filename != 'settings.py':
                        apath = os.path.join(maindir, filename)
                        result.append(apath)
        count += 1
    return result[::-1]


setup(
    ext_modules=cythonize(all_path('.'))
)

for name in all_path('.'):
    os.remove(name.replace('.py', '.c'))
    os.remove(name)

shutil.rmtree('build')

运行命令

python ./setup_c.py build_ext --inplace
原文地址:https://www.cnblogs.com/beihangxuwei/p/13825378.html