How to use ctypes.util.find_library to import .so libraries in AWS lambda (python)?

社会主义新天地 提交于 2020-01-15 10:27:08

问题


What I'm trying

A python package I'm using (OCRMYPDF) on Lambda needs the leptonica library liblept.so.5. On isolating the import code I found the issue is with find_library('lept'). Printing the result returns None.

from ctypes.util import find_library
def lambda_handler(event, context):
    liblept=find_library('lept')
    print("liblept:%s"%liblept)

The python package I'm using needs many native compiled dependencies. I'm trying to import these using lambda layers.

layer structure

/opt/
  /opt/bin/
  /opt/lib/
    /opt/lib/liblept.so.5
  /opt/tesseract

I'm able to access the file using CDLL (code below). But I don't want to rewrite the package and replace every find_library() with CDLL. Is it possible to set the import directory for find_library?

liblept=CDLL("/opt/lib/liblept.so.5") # found
print("liblept:%s"%liblept)

My layer code works

I used a docker image to build layer. The files in /opt/bin that depend on leptonica are working (tesseract runs properly, tested OCR as well).

logging.info(os.system("tesseract --version"))

output

START RequestId: d826d36c-4ce9-4b67-b501-8c9042edcf80 Version: $LATEST
tesseract 4.1.0
 leptonica-1.78.0
  libgif 5.1.4 : libjpeg 6b (libjpeg-turbo 1.2.90) : libpng 1.2.49 : libtiff 4.0.3 : zlib 1.2.8 : libwebp 0.3.0
 Found AVX
 Found SSE
END RequestId: d826d36c-4ce9-4b67-b501-8c9042edcf80

来源:https://stackoverflow.com/questions/59083051/how-to-use-ctypes-util-find-library-to-import-so-libraries-in-aws-lambda-pytho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!