安装第三方库
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)
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:
"""
file_type = '*.py'
paths = get_file_path(path_dir, file_type)
for pa in paths:
new_str = str_replace(pa, 'py', 'pyx')
os.rename(pa, new_str)
old_name = path_dir + r'\main.pyx'
new_name = path_dir + r'\main.py'
os.rename(old_name,new_name)
os.system("cd {} && easycython *.pyx ".format(path_dir))
file_type = '*.html'
paths = get_file_path(path_dir, file_type)
for pa in paths:
os.remove(pa)
file_type = '*.pyx'
paths = get_file_path(path_dir, file_type)
for pa in paths:
os.remove(pa)
file_type = '*.c'
paths = get_file_path(path_dir, file_type)
for pa in paths:
os.remove(pa)
file_type = '*.pyd'
paths = get_file_path(path_dir, file_type)
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)
|