將python專案轉化為so檔案

2021-08-20 04:20:26 字數 2970 閱讀 8782

1.安裝gccsudo apt-get build-dep gcc

安裝cythonpip install cython

2.編譯整個目錄:

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

setup.py**如下:

#-* -coding: utf-8 -* -

__author__ = 'arvin'

"""執行前提:

系統安裝python-devel 和 gcc

python安裝cython

編譯整個當前目錄:

python py-setup.py

編譯某個資料夾:

python py-setup.py bigomodel

生成結果:

目錄 build 下

生成完成後:

啟動檔案還需要py/pyc擔當,須將啟動的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"

defgetpy

(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)

ifnot 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')

3.生成結果:

so檔案在build目錄下

shylin

Python 專案轉化為so檔案

思路是先將py轉換為c 然後編譯c為so檔案,所以要安裝以下內容 python 安裝 cython pip install cythonlinux 安裝 python devel,gcc yum install python devel yum install gcc 新建test.py檔案,內容如...

Python將json轉化為sql語句

呼叫阿里雲資料庫監控資料返回的json資料 案例資料 apidate key mysql detailedspaceusage unit mb valueformat ins size data size log size tmp size other size key mysql iops uni...

將體能轉化為智慧型

將體能轉化為智慧型創造力源於智慧型,其基礎建於體力之上。但是體能的發展只是增加你智慧型的輸出量。你如何將體能轉化為智慧型呢?以下有幾個方法 1.放棄消極思想 這句話做起來並非像聽起來那麼容易,我們都難免會有某一程度的消極思想,它是我們失敗的藉口和自我安慰的理由,讓我們企圖迴避挑戰。當你的心中滿是消極...