python将项目打包成so文件2

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

例如:

一、

1.引入库

代码如下(示例):

# -*- coding:utf-8 -*-
# author: cbj
# datetime: 2024/7/2 16:01
# @File: 134打包python项目so文件2.py

import os
import sys
import shutil
import numpy
import tempfile
from pathlib import Path
from setuptools import setup
from setuptools.extension import Extension

from Cython.Build import cythonize
from Cython.Distutils import build_ext
import platform

# code from:   https://blog.youkuaiyun.com/qq_33375598/article/details/118677130

# 构建后存放的文件目录
build_root_dir = 'build/lib.' + platform.system().lower() + '-' + platform.machine() + '-' + str(
    sys.version_info.major) + '.' + str(sys.version_info.minor)

print(build_root_dir)

extensions = []
ignore_folders = ['build', 'test', 'tests', '.idea', '__pycache__', 'static', 'migrations']
ignore_files = ['setup.py', 'main.py', 'manage.py', "main_new.py", "main_new2.py", "clean.py", "jiami.py",
                "simplepro.lic", "jiami2.py"]

conf_folders = ['conf']
ignore_suffix = ['.c', '.pyc', '.o', '.log']


def get_root_path(root):
    if os.path.dirname(root) in ['', '.']:  # 得到文件的文件路径
        return os.path.basename(root)  # 返回path最后的文件名
    else:
        return get_root_path(os.path.dirname(root))


def copy_file(src, dest):
    if os.path.exists(dest):  # 目的文件存在返回
        return
    if not os.path.exists(os.path.dirname(dest)):  # 目的文件夹不存在,递归创建文件夹
        os.makedirs(os.path.dirname(dest))
    if os.path.isdir(src):  # 判断某一路径是否为目录
        shutil.copytree(src, dest)  # 拷贝整个文件夹(目的文件夹需要不存在,否则会失败)
    else:
        shutil.copyfile(src, dest)  # 拷贝整个文件


def touch_init_file():  # 在临时文件夹中创建init
    init_file_name = os.path.join(tempfile.mkdtemp(), '__init__.py')
    with open(init_file_name, 'w'):
        pass
    return init_file_name


init_file = touch_init_file()
print(init_file)


def compose_extensions(root='.'):
    for file_ in os.listdir(root):  # 当前目录下的所有文件
        abs_file = os.path.join(root, file_)  # 路径拼接
        if os.path.isfile(abs_file):
            absfile1 = Path(abs_file)
            filename = absfile1.name
            filesuffix = absfile1.suffix
            if abs_file.endswith('.py') and filename not in ignore_files:
                extensions.append(Extension(get_root_path(abs_file) + '.*', [abs_file]))
            elif filesuffix in ignore_suffix:
                continue
            else:
                copy_file(abs_file, os.path.join(build_root_dir, abs_file))
            # if abs_file.endswith('__init__.py'):  # 用空白的__init__.py替代原有的
            #     copy_file(init_file, os.path.join(build_root_dir, abs_file))
        else:

            if os.path.basename(abs_file) in ignore_folders:  # 忽略的文件不拷贝
                continue
            if os.path.basename(abs_file) in conf_folders:  # 配置文件一同拷贝
                copy_file(abs_file, os.path.join(build_root_dir, abs_file))
            compose_extensions(abs_file)


compose_extensions()
os.remove(init_file)

setup(
    name='your_project_name',
    version='1.0',
    ext_modules=cythonize(
        extensions,
        nthreads=16,
        compiler_directives=dict(always_allow_keywords=True),
        include_path=[numpy.get_include()],
        language_level="3"),
    cmdclass=dict(build_ext=build_ext))

# 执行命令
# python setup.py build_ext


# if __name__ == '__main__':
#     root= r'D:\data1\20240701\jiami\20240110\data_augmentation20230914\data_augmentation'
#     compose_extensions(root)

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值