欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

思路是先將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)行后會生成build文件夾,如下

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

集成編譯

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

1.文件夾編譯

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

3.刪除編譯的temp文件夾

將需要編譯的目錄和setup.py放在同一層級,執(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í)會出問題,暫時(shí)沒有解決,還需要使用pyc代替

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

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

相關(guān)文章

  • Python使用itertools模塊實(shí)現(xiàn)排列組合功能示例

    Python使用itertools模塊實(shí)現(xiàn)排列組合功能示例

    這篇文章主要介紹了Python使用itertools模塊實(shí)現(xiàn)排列組合功能,涉及Python基于itertools模塊product、permutations與combinations_with_replacement方法進(jìn)行排列、組合等相關(guān)操作實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-07-07
  • 解決Keras 自定義層時(shí)遇到版本的問題

    解決Keras 自定義層時(shí)遇到版本的問題

    這篇文章主要介紹了解決Keras 自定義層時(shí)遇到版本的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • pytorch中unsqueeze用法小結(jié)

    pytorch中unsqueeze用法小結(jié)

    unsqueeze()的作用是用來增加給定tensor的維度的,本文主要介紹了pytorch中unsqueeze用法小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04
  • Python如何批量生成和調(diào)用變量

    Python如何批量生成和調(diào)用變量

    這篇文章主要介紹了Python如何批量生成和調(diào)用變量,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-11-11
  • python教程之利用pyautogui圖形自動(dòng)化擊敗重復(fù)性辦公任務(wù)

    python教程之利用pyautogui圖形自動(dòng)化擊敗重復(fù)性辦公任務(wù)

    在使用Python做腳本的話,有兩個(gè)庫可以使用,一個(gè)為PyUserInput庫,另一個(gè)為pyautogui庫,就本人而言更喜歡使用pyautogui庫,該庫功能多,使用便利,下面這篇文章主要給大家介紹了關(guān)于python教程之利用pyautogui圖形自動(dòng)化擊敗重復(fù)性辦公任務(wù)的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • keras做CNN的訓(xùn)練誤差loss的下降操作

    keras做CNN的訓(xùn)練誤差loss的下降操作

    這篇文章主要介紹了keras做CNN的訓(xùn)練誤差loss的下降操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • python如何求2-1000內(nèi)的所有回文素?cái)?shù)

    python如何求2-1000內(nèi)的所有回文素?cái)?shù)

    這篇文章主要介紹了python如何求2-1000內(nèi)的所有回文素?cái)?shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • python運(yùn)行加速的幾種方式

    python運(yùn)行加速的幾種方式

    Python運(yùn)行的慢是歷來被詬病的,本文就來介紹一下python運(yùn)行加速的幾種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Python?數(shù)據(jù)類型中的字符串和數(shù)字

    Python?數(shù)據(jù)類型中的字符串和數(shù)字

    這篇文章主要介紹了Python?數(shù)據(jù)類型中的字符串和數(shù)字,Python3中有六個(gè)標(biāo)準(zhǔn)的數(shù)據(jù)類型,Number、String、List、Tuple、Set、Dictionary,加先來我們就來看看這幾種數(shù)據(jù)類型的具體相關(guān)介紹,需要的小伙伴可以參考一下
    2022-02-02
  • Python lambda表達(dá)式原理及用法解析

    Python lambda表達(dá)式原理及用法解析

    這篇文章主要介紹了Python lambda表達(dá)式原理及用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08

最新評論