python源码 .py文件转.pyd文件

安装第三方库

pip install easycython

test.py

下面展示一些 内联代码片

def test():
    print("调用成功")

main.py

import test
test.test()

test.py文件重命名为:test.pyx

在这里插入图片描述

终端运行命令

easycython *.pyx

在这里插入图片描述

重命名pyd文件,删除:cp36-win_amd64.

在这里插入图片描述

运行代码,调用成功:

在这里插入图片描述

代码实现

import glob
import os

def get_file_path(path, file_type):
    """
    取文件夹下相同后缀文件路径列表
    :param path: 文件夹路径
    :param file_type: 后缀名
    :return:文件路径列表
    """
    paths = glob.glob(os.path.join(path,
                                   file_type
                                   ))
    return paths
def str_replace(old_str,old,new):
    """
    在 old_str 字符串中,把 old 替换成 new
    :param old_str:原字符串
    :param old:被替换的字符
    :param new:替换后的字符
    :return:替换后的字符串
    """
    new_str = old_str.replace(old,new)
    # print("new_str:",new_str)
    return new_str

def py_to_pyd(path_dir,middle_name_pyd):
    """
    .py 文件 转 .pyd文件
    :param path_dir:.py文件所在的文件夹路径
    :param middle_name_pyd:pyd的中间名,如: .cp36-win_amd64
    :return:
    """
    # .py 文件 转成 .pyx文件
    file_type = '*.py'
    paths = get_file_path(path_dir, file_type)
    # print(paths)
    for pa in paths:
        new_str = str_replace(pa, 'py', 'pyx')
        os.rename(pa, new_str)
    # main.pyx 转成 main.py
    old_name = path_dir + r'\main.pyx'
    new_name = path_dir + r'\main.py'
    os.rename(old_name,new_name)
    # 把 pyx 转成 pyd
    os.system("cd {} && easycython *.pyx ".format(path_dir))
    # 删除所有 .html 文件
    file_type = '*.html'
    paths = get_file_path(path_dir, file_type)
    # print(paths)
    for pa in paths:
        os.remove(pa)
    # 删除所有 .pyx 文件
    file_type = '*.pyx'
    paths = get_file_path(path_dir, file_type)
    # print(paths)
    for pa in paths:
        os.remove(pa)
    # 删除所有 .c 文件
    file_type = '*.c'
    paths = get_file_path(path_dir, file_type)
    # print(paths)
    for pa in paths:
        os.remove(pa)
    # 删除名字中的 .pyd文件 中的 cp36-win_amd64
    file_type = '*.pyd'
    paths = get_file_path(path_dir, file_type)
    # print(paths)
    for pa in paths:
        new_str = str_replace(pa, middle_name_pyd, '')
        os.rename(pa,new_str)
if __name__ == '__main__':
    path_dir = r"C:\Users\xiahuadong\Desktop\number_humen - 副本"
    middle_name_pyd = '.cp36-win_amd64'
    py_to_pyd(path_dir, middle_name_pyd)

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值