pyd文件生成
安装easycython库
pip install easycython
test.py
def test(): print("调用成功")
main.py
import test test.test()
test.py文件重命名为:test.pyx
终端运行命令
easycython *.pyx
重命名pyd文件,删除:cp36-win_amd64.
运行代码,调用成功:
python main.py
代码实现
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 pa<i>本文来源gaodai$ma#com搞$$代**码网</i>ths 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)
补充:Python 常见文件格式 .py .pyc .pyw .pyo .pyd 之间的主要区别
Python([ˈpaɪθən])是一种面向对象、解释型计算机程序设计语言。Python语法简洁、清晰,具有丰富和强大的类库。
Python源代码遵循GPL(GNU General Public License)协议,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。
Python常被称为胶水语言,能把用其他语言编写的各模块(尤其是C/C++)轻松地联结在一起。常见情形是,用Python快速生成程序原型(有时甚至是程序最终界面),然后对其中有特别要求的部分,用更合适的语言改写;譬如:3D游戏中的图形渲染模块,性能要求特别高,就可用C/C++重写,而后封装为Python可调用的扩展类库。需要注意的是,在您使用扩展类库时可能需要考虑平台问题,某些扩展类库可能不提供跨平台实现。