最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python 項(xiàng)目轉(zhuǎn)化為so文件實(shí)例

 更新時(shí)間:2019年12月23日 11:39:13   作者:gmHappy  
今天小編就為大家分享一篇Python 項(xiàng)目轉(zhuǎn)化為so文件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

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

python 安裝:cython

pip install cython

linux 安裝:python-devel,gcc

yum install python-devel
yum install gcc

初步編譯

新建Test.py文件,內(nèi)容如下

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

  def say(self):
    print ('hello')

新建setup.py,內(nèi)容如下

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules = cythonize(["Test.py"]))

在bash中執(zhí)行

python setup.py build_ext

運(yùn)行后會(huì)生成build文件夾,如下

現(xiàn)在so文件就可以像普通py文件一樣導(dǎo)入了

集成編譯

做了以下內(nèi)容:

1.文件夾編譯

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

3.刪除編譯的temp文件夾

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

setup.py代碼如下:

'''
Created on 2019年3月27日

@author: hylink
'''
#-* -coding: UTF-8 -* -

"""
執(zhí)行前提:
  系統(tǒng)安裝python-devel 和 gcc
  Python安裝cython

編譯整個(gè)當(dāng)前目錄:
  python py-setup.py
編譯某個(gè)文件夾:
  python py-setup.py BigoModel

生成結(jié)果:
  目錄 build 下

生成完成后:
  啟動(dòng)文件還需要py/pyc擔(dān)當(dāng),須將啟動(dòng)的py/pyc拷貝到編譯目錄并刪除so文件

"""

import sys, os, shutil, time
from distutils.core import setup
from Cython.Build import cythonize

starttime = 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')

注意問題

1.編譯后執(zhí)行需要相同的python版本和編碼

2.py中使用__file__內(nèi)置變量的文件編譯后調(diào)用時(shí)會(huì)出問題,暫時(shí)沒有解決,還需要使用pyc代替

3.使用時(shí)注意權(quán)限控制

以上這篇Python 項(xiàng)目轉(zhuǎn)化為so文件實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python實(shí)現(xiàn)批量視頻分幀、保存視頻幀

    python實(shí)現(xiàn)批量視頻分幀、保存視頻幀

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)批量視頻分幀、保存視頻幀,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Python中求對(duì)數(shù)方法總結(jié)

    Python中求對(duì)數(shù)方法總結(jié)

    這篇文章主要介紹了Python中求對(duì)數(shù)方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • pycharm中連接mysql數(shù)據(jù)庫(kù)的步驟詳解

    pycharm中連接mysql數(shù)據(jù)庫(kù)的步驟詳解

    在進(jìn)行Python研發(fā)的時(shí)候,pycharm是一個(gè)很好的IDE,下面這篇文章主要給大家介紹了pycharm中連接mysql數(shù)據(jù)庫(kù)的步驟,文中通過圖文介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • Python 解決空列表.append() 輸出為None的問題

    Python 解決空列表.append() 輸出為None的問題

    在本篇文章里小編給大家整理了一篇關(guān)于Python 解決空列表.append() 輸出為None的問題的相關(guān)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-05-05
  • python遠(yuǎn)程連接服務(wù)器MySQL數(shù)據(jù)庫(kù)

    python遠(yuǎn)程連接服務(wù)器MySQL數(shù)據(jù)庫(kù)

    這篇文章主要為大家詳細(xì)介紹了python遠(yuǎn)程連接服務(wù)器MySQL數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • python 匹配url中是否存在IP地址的方法

    python 匹配url中是否存在IP地址的方法

    今天小編就為大家分享一篇python 匹配url中是否存在IP地址的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Python Django 實(shí)現(xiàn)簡(jiǎn)單注冊(cè)功能過程詳解

    Python Django 實(shí)現(xiàn)簡(jiǎn)單注冊(cè)功能過程詳解

    這篇文章主要介紹了Python Django 實(shí)現(xiàn)簡(jiǎn)單注冊(cè)功能過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Python根據(jù)過濾器拆分列表

    Python根據(jù)過濾器拆分列表

    這篇文章主要介紹了Python根據(jù)過濾器拆分列表,利用Python代碼實(shí)現(xiàn)代通過過濾器拆分列表的功能。文章圍繞其相關(guān)資料展開詳細(xì)內(nèi)容,需要的朋友可以參考一下
    2021-11-11
  • tensorflow構(gòu)建BP神經(jīng)網(wǎng)絡(luò)的方法

    tensorflow構(gòu)建BP神經(jīng)網(wǎng)絡(luò)的方法

    這篇文章主要為大家詳細(xì)介紹了tensorflow構(gòu)建BP神經(jīng)網(wǎng)絡(luò)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Python緩存技術(shù)實(shí)現(xiàn)過程詳解

    Python緩存技術(shù)實(shí)現(xiàn)過程詳解

    這篇文章主要介紹了Python緩存技術(shù)實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09

最新評(píng)論

广安市| 拉孜县| 乌兰浩特市| 高淳县| 建水县| 麻栗坡县| 阳西县| 宿迁市| 泸定县| 阿瓦提县| 台湾省| 贵定县| 五华县| 刚察县| 威海市| 独山县| 巴中市| 潢川县| 盐边县| 南充市| 汶上县| 防城港市| 聂荣县| 灌南县| 湘西| 银川市| 吴堡县| 斗六市| 红河县| 平和县| 安宁市| 广平县| 广州市| 林周县| 靖宇县| 昌乐县| 黄平县| 深州市| 嘉荫县| 丹棱县| 宣威市|