精品网页,甄嬛传mp4下载,潜江人论坛
将python代码编译成pyd文件,以保护代码。(注,只能单个py文件生成单个pyd文件,windows下为pyd文件,linux下为so文件)
网上找到一篇相关的博文 使用cython将py编译成.so文件 ,从介绍、环境、注意事项、代码步骤注释得十分详细,因为代码不太符合自己的要求,因此基于该文代码重新写了一份代码。
代码要求:
代码步骤(同):
注意事项:
源代码(放在项目根目录下):
#!/usr/bin/env python # -*- coding: utf-8 -*- # @file : build.py # @author: wade cheung, editby bh liu # @date : 2019/2/23 # @desc : 使用cython.build.cythonize将py编译成.so文件 import sys import os import shutil from distutils.core import setup from cython.build import cythonize currdir = os.path.abspath('.') + '\\' parentpath = sys.argv[1] if len(sys.argv) > 1 else "" setupfile = os.path.join(os.path.abspath('.'), __file__) build_dir = "build" build_tmp_dir = build_dir + "/temp" filter_dir_set = {'dist', 'build', 'config', 'data', 'const', 'test', 'orm\\data_init'} except_files = { __file__, 'build.py', 'build_load_db.py', 'main.py', 'frozen_dir.py', 'utils\\time_it.py', } def filter_file(file_name): if file_name.__contains__(currdir): file_name = file_name.replace(currdir, '') if file_name in except_files: # 过滤文件 return true file_path = file_name.split("\\") if len(file_path) > 1: file_dir = "" for i in range(len(file_path)-1): file_dir = os.path.join(file_dir, file_path[i]) if file_dir in filter_dir_set: return true return file_path[0] in filter_dir_set def getpy(basepath=os.path.abspath('.'), parentpath='', name='', copyother=false, delc=false): """ 获取py文件的路径 :param basepath: 根路径 :param parentpath: 父路径 :param name: 文件/夹 :param copy: 是否copy其他文件 :return: py文件的迭代器 """ fullpath = os.path.join(basepath, parentpath, name) for fname in os.listdir(fullpath): ffile = os.path.join(fullpath, fname) if os.path.isdir(ffile) and fname != build_dir and not fname.startswith('.'): for f in getpy(basepath, os.path.join(parentpath, name), fname, copyother, delc): yield f elif os.path.isfile(ffile): ext = os.path.splitext(fname)[1] # 删除.c 临时文件 if ext == ".c": if delc: os.remove(ffile) elif not filter_file(ffile) and (ext not in ('.pyc', '.pyx') and ext in ('.py', '.pyx') and not fname.startswith('__')): yield os.path.join(parentpath, name, fname) elif copyother and ext not in ('.pyc', '.pyx'): # 复制其他文件到./build 目录下 dstdir = os.path.join(basepath, build_dir, parentpath, name) if not os.path.isdir(dstdir): os.makedirs(dstdir) shutil.copyfile(ffile, os.path.join(dstdir, fname)) else: pass # 获取py列表 module_set = set(getpy(basepath=currdir, parentpath=parentpath)) ## 编译成.so文件 try: setup(ext_modules=cythonize(module_set, language_level=3), script_args=["build_ext", "-b", build_dir, "-t", build_tmp_dir]) pass except exception as ex: print("error! ", str(ex)) else: # 复制其他文件到./build 目录下 getpy(basepath=currdir, parentpath=parentpath, copyother=true) # 删除临时文件 ~ getpy(basepath=currdir, parentpath=parentpath, delc=true) if os.path.exists(build_tmp_dir): shutil.rmtree(build_tmp_dir) print("done !")
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python爬虫:Request Payload和Form Data的简单区别说明
浅谈Python中threading join和setDaemon用法及区别说明
Python3-异步进程回调函数(callback())介绍
python继承threading.Thread实现有返回值的子类实例
Python中使用threading.Event协调线程的运行详解
网友评论