麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > Python > 正文

Python 項目轉化為so文件實例

2024-09-09 19:03:26
字體:
來源:轉載
供稿:網友

思路是先將py轉換為c代碼,然后編譯c為so文件,所以要安裝以下內容:

python 安裝:cython

pip install cython

linux 安裝:python-devel,gcc

yum install python-develyum install gcc

初步編譯

新建Test.py文件,內容如下

class test:    def __init__(self):    print('init')  def say(self):    print ('hello')

新建setup.py,內容如下

from distutils.core import setupfrom Cython.Build import cythonizesetup(ext_modules = cythonize(["Test.py"]))

在bash中執行

python setup.py build_ext

運行后會生成build文件夾,如下

現在so文件就可以像普通py文件一樣導入了

集成編譯

做了以下內容:

1.文件夾編譯

2.刪除編譯出的.c文件

3.刪除編譯的temp文件夾

將需要編譯的目錄和setup.py放在同一層級,執行python setup.py,so文件在build目錄下

setup.py代碼如下:

'''Created on 2019年3月27日@author: hylink'''#-* -coding: UTF-8 -* -"""執行前提:  系統安裝python-devel 和 gcc  Python安裝cython編譯整個當前目錄:  python py-setup.py編譯某個文件夾:  python py-setup.py BigoModel生成結果:  目錄 build 下生成完成后:  啟動文件還需要py/pyc擔當,須將啟動的py/pyc拷貝到編譯目錄并刪除so文件"""import sys, os, shutil, timefrom distutils.core import setupfrom Cython.Build import cythonizestarttime = time.time()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"def getpy(basepath=os.path.abspath('.'), parentpath='', name='', excepts=(), copyOther=False,delC=False):  """  獲取py文件的路徑  :param basepath: 根路徑  :param parentpath: 父路徑  :param name: 文件/夾  :param excepts: 排除文件  :param copy: 是否copy其他文件  :return: py文件的迭代器  """  fullpath = os.path.join(basepath, parentpath, name)  for fname in os.listdir(fullpath):    ffile = os.path.join(fullpath, fname)    #print basepath, parentpath, name,file    if os.path.isdir(ffile) and fname != build_dir and not fname.startswith('.'):      for f in getpy(basepath, os.path.join(parentpath, name), fname, excepts, copyOther, delC):        yield f    elif os.path.isfile(ffile):      ext = os.path.splitext(fname)[1]      if ext == ".c":        if delC and os.stat(ffile).st_mtime > starttime:          os.remove(ffile)      elif ffile not in excepts and os.path.splitext(fname)[1] not in('.pyc', '.pyx'):        if os.path.splitext(fname)[1] in('.py', '.pyx') and not fname.startswith('__'):          yield os.path.join(parentpath, name, fname)        elif copyOther:            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_list = list(getpy(basepath=currdir,parentpath=parentpath, excepts=(setupfile)))try:  setup(ext_modules = cythonize(module_list),script_args=["build_ext", "-b", build_dir, "-t", build_tmp_dir])except Exception as e:  print (e)else:  module_list = list(getpy(basepath=currdir, parentpath=parentpath, excepts=(setupfile), copyOther=True))module_list = list(getpy(basepath=currdir, parentpath=parentpath, excepts=(setupfile), delC=True))if os.path.exists(build_tmp_dir): shutil.rmtree(build_tmp_dir)print ("complate! time:", time.time()-starttime, 's')
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 一级外国毛片 | 国产精品jk白丝蜜臀av软件 | 久久久久久久久久久亚洲 | 99爱国产精品 | 成人在线观看小视频 | 三人弄娇妻高潮3p视频 | 嗯哈~不行好大h双性 | 性生活香蕉视频 | 久产久精品 | 亚洲精品 在线播放 | 夜夜夜操操操 | 久久国产精品久久久久久 | 日韩在线欧美在线 | 黄色的视频在线观看 | 国产一区二区在线免费 | 欧美人成在线视频 | 李宗瑞国产福利视频一区 | 男女亲热网站 | 91成人在线网站 | 99欧美视频 | av在线播放网址 | 亚洲一区二区三区视频免费 | 久久久婷婷一区二区三区不卡 | 久国产精品视频 | 在线亚洲免费视频 | www.54271.com| 欧美激情视频一区二区免费 | 国产亚洲区 | 色妞妞视频 | 一级大片一级一大片 | 羞羞色在线观看 | 99ri精品 | 最近高清无吗免费看 | 黄色片视频在线观看 | 亚洲自拍第二页 | 国产一区二区三区四区在线 | 久久吊 | 国产成人精品免高潮在线观看 | 久久久久电影网站 | 亚洲无限资源 | 一级视频在线播放 |