首页 > 其他分享 >py编译成pyd文件

py编译成pyd文件

时间:2024-07-31 20:51:21浏览次数:10  
标签:files os 编译成 py file path pyd dir

 

 

该踩的的坑都踩过了

最简单的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"))

 

标签:files,os,编译成,py,file,path,pyd,dir
From: https://www.cnblogs.com/shizhengwen/p/18335449

相关文章

  • Python - Strategies to handle exceptions in your code
    Therearetwoapproachesthatcanbefollowedwhenwewanttodealwithexceptionsthatoccurduetounusualevents:LBYL-LookBeforeYouLeapEAFP-EasiertoAskforForgivenessthanPermissionIntheLBYLapproach,weavoidexceptions,whileinthe......
  • 全网最适合入门的面向对象编程教程:29 类和对象的Python实现-断言与防御性编程和help函
    全网最适合入门的面向对象编程教程:29类和对象的Python实现-断言与防御性编程和help函数的使用摘要:在Python中,断言是一种常用的调试工具,它允许程序员编写一条检查某个条件。本文主要介绍了断言的应用场景和特点以及assert语句的使用,同时介绍了防御性编程和help()函数......
  • Python入门知识点 10--闭包与装饰器
    一、直接与间接程序开发潜规则生活中:   有台电脑,他的硬盘空间不够了,里面的资料我不能动,也不能删,咋办   1.加装硬盘--直接解决--前提是我的电脑能加装   2.插个u盘--间接解决-->没有特别的要求,比较灵活   开发潜规则:   代码拓展-->开放-->可......
  • python log运算如何写
    Python中用于计算对数的log()方法,是Python入门基础中的必会的方法,需要的朋友可以参考下。log()方法返回x的自然对数,x>0。语法以下是log()方法的语法:import math math.log( x )注意:此函数是无法直接访问的,所以我们需要导入math模块,然后需要用math的静态对象来调用......
  • Python - operator module
    >>>list(map(lambdax,y:x*y,[1,2,3,4],[5,6,7,8]))[5,12,21,32]Herewehavecalledthemapfunctionandsentalambdaexpressionasfirstargument.Insteadofthelambdafunction,youcansendoperator.mul.>>>list(map(operator......
  • Python操作excel常用操作介绍,入门首选
            使用Python操作Excel文件有许多常用操作。以下是一些常见的操作及其简要描述,下面是全面详细的示例,展示如何使用Python操作Excel文件,我们将使用pandas和openpyxl库来进行各种操作。常用库pandas:用于数据分析和处理,支持读取和写入Excel文件。openpyxl:用于读......
  • Python蒙特卡罗(Monte Carlo)模拟计算投资组合的风险价值(VaR)
    原文链接:http://tecdat.cn/?p=22862原文出处:拓端数据部落公众号如何使用Python通过蒙特卡洛模拟自动计算风险值(VaR)来管理投资组合或股票的金融风险。金融和投资组合风险管理中的VaR?VaR是"风险价值"的缩写,是许多公司和银行用来确定其公司内部金融风险水平的工具。风险值是为......
  • Python 元类深析:定制类创建的高级工
    元类是Python中一个强大而高级的特性,它允许我们自定义类的创建过程。本文将详细介绍Python元类的概念、工作原理以及常见用途。一. 什么是元类简单来说,元类就是用来创建类的类。在Python中,类也是对象,而元类就是用来创建这些类对象的。我们知道类是用来创建对象的......
  • 【视频讲解】Python用LSTM、Wavenet神经网络、LightGBM预测股价
    原文链接:https://tecdat.cn/?p=37184原文出处:拓端数据部落公众号 分析师:YuyanYe在金融科技的浪潮中,量化投资方法以其数据驱动和模型导向的特性,日益成为资本市场分析的重要工具。特别是,长短期记忆网络(LSTM)、Wavenet以及LightGBM等先进的机器学习算法,因其在时间序列预测中的卓......
  • Python写UI自动化--playwright(点击操作)
    本篇介绍playwright点击操作,click()方法的常用参数目录0.selector(必需)1.modifiers(可选)2.position(可选)3.button(可选)4.click_count(可选)5.delay6.timeout(可选)7.force=True(可选)8.trial=True(可选)9.no_wait_after(可选)注意事项0.selecto......