py编译成pyd文件

 

 

该踩的的坑都踩过了

最简单的demo

 

# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: Irving Shi
"""
# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: Irving Shi
"""
# setup.py
from setuptools import setup
from Cython.Build import cythonize
import os
import shutil

def find_pyx(path):
    pyx_files = []
    for root, dirs, files in os.walk(path):
        if "venv" in root: continue
        for file in files:
            # print(file)
            if file.endswith(".c") or file.endswith(".pyd"):
                # print(os.path.join(root, file))
                os.remove(os.path.join(root, file))
    return pyx_files

def find_py(path):

    not_py_files = []
    py_files = []
    for root, dirs, files in os.walk(path):
        if "venv" in root: continue
        if "__pycache__" in root: continue
        if ".git" in root: continue
        if ".idea" in root: continue
        if "setup.py" in files:  continue
        if "migrations" in root:  continue
        if "build" in root:  continue
        if "test" in root:  continue
        for file in files:
            if file.endswith(".py"):

                py_files.append(os.path.join(root, file))
            elif file.endswith(".c") or file.endswith(".pyd"):
                continue
            else:
                not_py_files.append(os.path.join(root, file))
    return py_files, not_py_files

d_dir = r"E:\_bigo\bigo\client_django"
to_dir=r"E:\_bigo\bigo\client_django2"
ext_files, not_py_files = find_py(d_dir)
for py_file in ext_files:
    setup(
        ext_modules = cythonize(py_file, compiler_directives={'language_level': "3"}),  # "myapp" 是包含 .py 文件的文件夹名
        script_args=["build_ext",  f"--build-lib={to_dir}"],
        options={
            'build_ext': {
                'build_lib': os.path.dirname(py_file).replace(d_dir, to_dir)
            }
        }
    )
for not_py_file in not_py_files:
    # print(not_py_file, not_py_file.replace(d_dir, to_dir))
    destination_dir = os.path.dirname(not_py_file.replace(d_dir, to_dir,1))
    if not os.path.exists(destination_dir):
        os.makedirs(destination_dir)
    shutil.copy(not_py_file, not_py_file.replace(d_dir, to_dir, 1))

try:
    shutil.rmtree(os.path.join(d_dir, "build"))
    find_pyx(d_dir)
except Exception:
    pass

shutil.copy(os.path.join(d_dir, "manage.py"), os.path.join(to_dir, "manage.py"))

 

posted @ 2024-07-31 20:47  陨落&新生  阅读(20)  评论(0编辑  收藏  举报