python 패키지 생 성 so 파일 구현

1.설치 도구
python 3 설치:cython

pip3 install cython -i http://mirrors.aliyun.com/pypi/simple/ 
pip 국내 미 러 링
아 리 운http://mirrors.aliyun.com/pypi/simple/
중국과학기술대학https://pypi.mirrors.ustc.edu.cn/simple/
콩짜개(douban)http://pypi.douban.com/simple/
청화대학https://pypi.tuna.tsinghua.edu.cn/simple/
중국과학기술대학http://pypi.mirrors.ustc.edu.cn/simple/
linux 설치:python-devel,gcc
gcc 버 전 정보 보기,있 으 면 설치 하지 않 아 도 됩 니 다.

gcc --version
在这里插入图片描述
서버 버 전 환경 에 대응 하 는 gcc 컴 파일 설치 python-devel 이 자체 적 으로 설치 되 어 있 지 않 습 니 다.

pip3 install python-devel -i http://mirrors.aliyun.com/pypi/simple/
2.Linux 의.so 파일(테스트 용례)
테스트 폴 더 만 들 기

mkdir so_test
so_test 폴 더 아래 봉 인 될 파일 test.py 만 들 기

import datetime 
class DataCenter(): 
  def gettime(self): 
     print(datetime.datetime.now()) 
  def write_data(self): 
    print("hello XiaoBoData!")
so_test 폴 더 아래 호출 파일 so 만 들 기test.py

from test import DataCenter 
data = DataCenter() 
data.gettime() 
data.write_data()
우선 so 실행test 는 코드 가 성공 적 으로 실 행 될 수 있 도록 보증 한 후에 so 패 키 징 을 합 니 다.
在这里插入图片描述
so_test 폴 더 에서 압축 파일 setup.py 만 들 기

from test import DataCenter 
data = DataCenter() 
data.gettime() 
data.write_data()
최종 폴 더 아래:
在这里插入图片描述
집행
在这里插入图片描述
파일 크기 에 따라 컴 파일 시간 이 다 릅 니 다.
실행 에 성공 하면 같은 디 렉 터 리 에 test.c 파일 과 build 폴 더 가 생 성 됩 니 다.생 성 된 so 파일 은 build/lib.linux-aarch 64-3.5/디 렉 터 리 에 있 습 니 다.서버 환경 에 따라 생 성 된 so 파일 이름 도 다 르 고 생 성 위치 도 같 습 니 다.
在这里插入图片描述
생 성 된 so 파일 을 test.py 파일 저장 위치 로 복사 한 후 test.py,test.c,build 폴 더 를 삭제 하고 python 3 so 를 실행 합 니 다.성공 할 수 있 는 지 없 는 지 테스트 해 보 세 요.
在这里插入图片描述
테스트 증명 에 문제 가 없습니다.암호 화 된 so 파일=py 파일 은 명문 에서 비밀문서 로 바 뀌 었 을 뿐 접미사 이름 은 py 에서 so 로 바 뀌 었 고 다른 것 은 변 하지 않 았 으 며 코드 의 안전성 에 이 르 렀 습 니 다.
 3.Windows 에서 Python 을 pyd 파일 로 봉 합 니 다(dll)
pyd 는 dll 입 니 다.
my test.py 와 dlltest.py 는 위 와 같 습 니 다.
setupDll.py 만 들 기

# !/usr/bin/env python 
# -*- coding: utf-8 -*- 
from distutils.core import setup 
from Cython.Build import cythonize 
from distutils.extension import Extension 
def main(): 
#              Extension(' ', [' ']) 
  extensions = [Extension('mytest', ['mytest.py'])] 
  setup(ext_modules=cythonize(extensions))
if __name__ == '__main__': 
  main()  
pycharm 의 terminal 에 Python setupDll build 입력ext,pyd 파일 가 져 오기
在这里插入图片描述
build 폴 더 에서.pyd 파일 호출 방식 이 변 하지 않 습 니 다.

# !/usr/bin/env python
# -*- coding: utf-8 -*-
#      pyd  ,    dll_test.py   :
import sys
sys.path.append('./build/lib.win-amd64-3.7/')
from mytest import DataCenter
def main():
  data = DataCenter()
  data.gettime()
  data.write_data()

if __name__ == '__main__':
  main()  
실행 결과
在这里插入图片描述
python 패키지 생 성 so 파일 의 실현 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 python 패키지 생 성 so 파일 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 많은 지원 바 랍 니 다!

좋은 웹페이지 즐겨찾기